创建一个动态URL并在Laravel中重定向到它


Create a Dynamic URL and Redirect to it in Laravel

所以基本上这就是我需要的。我有一个这样的路由器定义。

Route::get('view/{postId}', 'PostController@view');

如果我们请求的url是www.domain.com/view/4,则会触发上述路由器定义。但我想要的是,我想像www.domain.com/[category-of-post]/[titile-of-post](例如:www.domain.com/music/easy-chords-in-guitar)一样出现这个url。

PostController中,我将使用传递的id获得帖子,从而可以生成我需要的url。问题开始了。正如你所看到的,我需要重定向到一个动态的url,每个帖子看起来都不一样我想重定向到这个动态URL,但这些URL没有在routes.php中定义。

这在拉腊维尔可能吗?

简而言之,我需要的是,在调用Redirect::to('someurl') 之前,我想在运行时用我动态生成的url值更新Illuminate'Routing'RouteCollection::$route数组,并使用相应的控制器操作

如果你需要进一步澄清,我一定会做的。请给我你的建议。

它比您想象的要简单。

Route::get('{category}/{title}',['uses' => 'FooController@bar']);

这应该是路线列表中定义的最后一条路线。任何其他路线都应该高于这条路线

这将与www.domain.com/music/easy-chords-in-guitar 相匹配

休息路线根据需要进行定义。

例如

Route::get('/',['uses' => 'FooController@home']);
Route::get('about',['uses' => 'FooController@about']);
Route::get('contact',['uses' => 'FooController@contact']);
Route::get('{category}/{title}',['uses' => 'FooController@bar']);

路线:

    Route::get('action/{slug}', 'HomeController@actionredeemvoucher')->name('home.actionredeemvoucher');

控制器中的功能:

    public function actionredeemvoucher($slug)
    {    
       print_r($slug);  
    }