I would try instead of return redirect()->away($post->external_url); to return redirect($post->external_url);
Redirect post with external link
I'm trying to mask my external urls, in the first place when a post has an external link in the database and no original content for a single post page itself. But when it has an external link and an original content page itself, the external url should be masked (look like the post url) on that page and should be redirected as well to the external page when clicked.
So the visitor needs to see the post url when they hover it for example, but when they click on it they should be redirected.
This is what i got so far in my PostController, and this works for the index pages where i use the $post->url variable. I can't figure out how to make this external_url on a single post page redirect from the controller, because of the already existing if statement.
I hope you understand what i'm trying to achieve. Can anyone help me in the right direction?
<?php
namespace App\Http\Controllers;
use App\Models\Post;
class PostController
{
public function __invoke(Post $post)
{
$mediaItems = $post->getMedia('multi_collection');
if (! empty($post->external_url) && $post->original_content == 0) {
return redirect()->away($post->external_url);
}
return view('posts.show', compact('post', 'mediaItems'));
}
}
Please or to participate in this conversation.