在不更改URL路由路径的情况下,将Laravel变量转换为URL

在不更改URL路由路径的情况下,将Laravel变量转换为URL,url,laravel-5,url-routing,Url,Laravel 5,Url Routing,我是laravel的新手,对在url末尾添加一个db变量而不影响路径感兴趣。例如,如果用户登录并在数据库中分配了variable=hello,则url将读取。然而,我不想改变我的路线,所以它仍然在看/home而不是/home/hello 我尝试在web.php中更改路由,如下所示: Route::get('/home',function(){ return redirect('home/{{Auth::user()->variable}}'); }); Route::get('/h

我是laravel的新手,对在url末尾添加一个db变量而不影响路径感兴趣。例如,如果用户登录并在数据库中分配了variable=hello,则url将读取。然而,我不想改变我的路线,所以它仍然在看/home而不是/home/hello 我尝试在web.php中更改路由,如下所示:

Route::get('/home',function(){
    return redirect('home/{{Auth::user()->variable}}');
});
Route::get('/home/{{Auth::user()->variable}}', 'HomeController@index');
但这看起来很混乱,它并没有从数据库中提取我的变量。我相信还有更好的方法,我没有
能够找到。我不是在寻找一个完整的解决方案,只是一个指向教程或指向正确方向的链接。

您应该能够执行以下操作:

Route::get('/home',function(){
    return redirect('home/'.Auth::user()->variable);
});
Route::get('/home/{variable}', 'HomeController@index');
然后“variable”的值将在HomeController索引方法中可用

public function index($variable) { ... }