Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel重定向URL控制器_Php_Laravel - Fatal编程技术网

Php Laravel重定向URL控制器

Php Laravel重定向URL控制器,php,laravel,Php,Laravel,我想将url从/topic/{title}重定向到/topic/{category}/{title}。 因此,我尝试在routes中注册: Route::get('/topic/{title}',function(){ return redirect()->action('/topic/{category}/{title}','DetailController@index'); }); 但我犯了个错误 未定义操作App\Http\Controllers/topic/{catego

我想将url从/topic/{title}重定向到/topic/{category}/{title}。 因此,我尝试在routes中注册:

Route::get('/topic/{title}',function(){
    return redirect()->action('/topic/{category}/{title}','DetailController@index');
});
但我犯了个错误

未定义操作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          
                 ]);
      }
class DetailController扩展控制器
{
/**
*显示资源的列表。
*
*@return\light\Http\Response
*/
公共功能索引($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'))
->其中('publish','=','Y')
->其中('parent_id','=',0)
->其中,根据

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

因此,您需要像这样传递参数:

return redirect()->action(
    'DetailController@index', ['category' => <enter_value>, 'title' => <enter_value>]
);
返回重定向()->操作(
'DetailController@index“,[“类别”=>,“标题”=>]
);

我建议您使用两条路由和两个控制器。一个处理主题的详细信息,另一个处理“旧”url的重定向

例如:用户将访问由控制器处理的“/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');
它没有经过测试,因为我在当地没有安装laravel env。但我认为它应该可以工作。让我知道

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

未定义操作App\Http\Controllers/topic/{category}/{title}

您正在错误地使用重定向

Route::get('/topic/{title}',function(){
return redirect()->action('/topic/{category}/{title}','DetailController@index');
});
您只能提供操作控制器,而不能提供路由。并且目标路由必须存在。因此,正确的用法是:

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

无论如何,您应该将逻辑从路由中分离出来。您有用于此的控制器…即使是非常短的代码块。它将保持一切有序和清晰。在您的路由文件中,您应该只有路由。

解决了,我在修改后找到了方法。以下是代码:

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}");

});
Route::get('/topik/{title}',函数($title){
$article=DB::table('t_artikel')
->join('t_section','t_section.id_section','=','t_artikel.id_section'))
->其中('publish','=','Y')
->其中('parent_id','=',0)

->其中('tgl_pub','如果要重定向到URI,则不要使用
redirect()->action()
;而是使用
redirect()->to()

Route::get('/topic/{title}',函数($title){
$category='';//如何获取类别
返回redirect()->到(“/topic/{$category}/{$title}”);
});

嗯,但是我想把链接重定向到/topic/category/titleplease,你能和我们分享一下你的DetailController@index?还有,一个主题可以包含多少个类别?可能存在两个标题相同(但类别不同)的主题?在这种情况下,如果您编写/topic/title,您期望的行为是什么?仅供参考,旧url已经共享,因此我想将旧url(topic/mytitle)重定向到新url(topic/mycategory/mytitle)不共享新url,如果有用户访问我的旧url,它会自动重定向到新url。类别和标题是动态的