dsa45664 2015-02-15 14:42
浏览 24
已采纳

路由服务提供商中的重定向

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:

  1. If there is no slug set it should redirect to the slugged url
  2. Make sure the slug is correct (in case of update) and redirect to proper slugged url.

$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.

  • 写回答

1条回答 默认 最新

  • duankang5882 2015-02-15 15:27
    关注

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏