【问题标题】:Laravel Redirect URL ControllerLaravel 重定向 URL 控制器
【发布时间】:2018-04-18 16:46:57
【问题描述】:

我想将 URL 从 /topic/{title} 重定向到 /topic/{category}/{title}。 所以我尝试在路线中注册它:

Route::get('/topic/{title}',function(){
    return redirect()->action('/topic/{category}/{title}','DetailController@index');
});

但我得到了错误

Action App\Http\Controllers/topic/{category}/{title} 未定义。

有人可以帮我了解路线吗? 感谢您的提前。

这是我的控制器

class DetailController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index($section=0,$title = 0)
        {
            $detail = DB::table('t_artikel')
                ->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')
                //->join('t_kolom', 't_kolom.id', '=', 't_artikel.penalar')
                ->where('publish', '=', 'Y')
                ->where('parent_id', '=', 0)
                ->where('tgl_pub', '<=', date('Y-m-d H:i:s'))
                ->where('t_artikel.urltitle', '=', $title)
                ->select('t_artikel.*', 't_section.*', 't_artikel.urltitle as urlartikel')
                ->first();
            $kategori = DB::table('t_section')
                ->where('id_supsection', '=', 0)
                ->where('status', '=', 1)
                ->orderBy('position', 'asc')
                ->whereNotIn('id_section', [34])
                ->select('t_section.*')
                ->get();
            $page = 'Detail';
            return view ('detail.detail',
                ['detail'       => $detail,
                 'kategori'     => $kategori,
                 'page'         => $page          
                 ]);
      }

【问题讨论】:

  • 拜托,您能与我们分享您的 DetailController@index 中的内容吗?另外,有多少个类别可以有一个主题?是否可能存在 2 个标题相同(但类别不同)的主题?在这种情况下,如果你写 /topic/title 你会期待什么行为?
  • fyi,旧网址已经共享,所以我想将旧网址(主题/mytitle)重定向到我的新网址(主题/mycategory/mytitle)而不共享新网址,如果有用户访问我的旧网址,因此它会自动重定向到新网址。类别和标题是动态的

标签: php laravel


【解决方案1】:

根据documentation

如果你的控制器路由需要参数,你可以将它们作为第二个参数传递给操作方法

所以,你需要像这样传递参数:

return redirect()->action(
    'DetailController@index', ['category' => <enter_value>, 'title' => <enter_value>]
);

【讨论】:

  • 嗯,但我希望链接重定向到 /topic/category/title
【解决方案2】:

我建议您使用 2 个路由和 2 个控制器。一个处理主题的细节,另一个处理“旧”网址的重定向。

例如:用户将访问由控制器处理的“/topic/title”,该控制器将识别主题和类别,然后将使用

public function handleTopic($title){
    // the code here will recognize topic and category
    // and will just provide the stuff to show the right page
    // in the end will redirect to that controller
    return redirect()->action('DetailController@index', ['category' => $category, 'title' => $title]);
}

public function index($stuffYouNeed){
    // retrieve the rest of data you need to show the page
    // you already know the title and category (cause of previous controller)
    // in the end return the view with data
    return view ('detail.detail',['data' => $data]);
}

在您的路线中,您必须添加一条路线并编辑现有路线,例如:

Route::get('topic/{title}', 'DetailController@handleTopic')->name('handleTopic');

Route::get('topic/{category}/{title}', 'DetailController@index')->name('showTopic');

它没有经过测试,因为 atm 我没有在本地设置 laravel env。但我认为它应该工作。告诉我

编辑:我忘了解释为什么你会看到错误

Action App\Http\Controllers/topic/{category}/{title} 未定义。

您错误地使用了重定向

Route::get('/topic/{title}',function(){
return redirect()->action('/topic/{category}/{title}','DetailController@index');
});

你只能提供一个动作控制器,而不是一个路由。并且目的地路线必须存在。所以正确的用法是:

return redirect()->action('Controller@action');

无论如何,您应该将逻辑与路由分开。你有控制器……即使是非常短的代码块。它将使一切井井有条且清晰。在您的路线文件中,您应该只有路线。

【讨论】:

    【解决方案3】:

    解决了,修改后我找到了。这是代码:

    Route::get('/topik/{title}',function($title){    
        $article = DB::table('t_artikel')
                ->join('t_section', 't_section.id_section', '=', 't_artikel.id_section')          
                ->where('publish', '=', 'Y')
                ->where('parent_id', '=', 0)
                ->where('tgl_pub', '<=', date('Y-m-d H:i:s'))
                ->where('t_artikel.urltitle', '=', $title)
                ->select('t_artikel.*', 't_section.urltitle as urltitlesec', 't_artikel.urltitle as urlartikel')
                ->first();
         $kategori = $article->urltitlesec;
         return redirect("/topik/{$kategori}/{$title}");
    
    });
    

    【讨论】:

      【解决方案4】:

      如果你想重定向到一个URI,那么不要使用redirect()-&gt;action();改用redirect()-&gt;to()

      Route::get('/topic/{title}', function ($title) {
          $category = ''; // Get category some how
      
          return redirect()->to("/topic/{$category}/{$title}");
      });
      

      【讨论】:

        猜你喜欢
        • 2018-04-03
        • 2013-11-30
        • 2017-10-08
        • 1970-01-01
        • 2014-02-28
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        相关资源
        最近更新 更多