I'm currently using the route/model binding to check if there is a valid id for a Tag
in my database. It's pretty sweet so far but I would like to make sure that:
$router->bind('tag_id', function ($id) {
$tag = \App\Tag::getById($id);
// if ($tag->slug !== \Request::segment(3))
// {
// return redirect('/tags/' . $tag->id . '/' . $tag->slug);
// }
if ($tag instanceof \App\Tag)
{
return $tag;
}
throw new NotFoundHttpException;
});
That's my code so far, but no idea how to redirect out of the RouteServiceProvider
or if it's even possible form there. Is there anyway to do this. I know it would be quite easy for the /tags/:id
style url, but I would like to catch the incorrect slugs as well if possible.
I don't particularly like this way but you can trigger the redirect without having it to return from a controller action or route closure by calling send()
on it. (That works for all kind responses by the way)
if ($tag->slug !== \Request::segment(3))
{
redirect('/tags/' . $tag->id . '/' . $tag->slug)->send();
}