Laravel变量到URL而不改变url-路由路径

Laravel variable to URL without changing url-route path

我是 laravel 的新手,有兴趣在不影响路径的情况下将数据库中的变量添加到 url 的末尾。例如,如果用户登录并在数据库中分配了变量 = hello,url 将显示为 http://app.com/home/hello。但是我不希望这个改变我的路由,所以它仍然在寻找 /home 而不是 /home/hello 我尝试按如下方式更改 web.php 中的路由:

Route::get('/home',function(){
    return redirect('home/{{Auth::user()->variable}}');
});
Route::get('/home/{{Auth::user()->variable}}', 'HomeController@index');

但这看起来很乱,而且它没有从数据库中提取我的变量。我确定有更好的方法我没有去过 能够找到。我不是在寻找完整的解决方案,只是 link 教程或​​指向正确方向的指南。

您应该可以执行以下操作:

Route::get('/home',function(){
    return redirect('home/'.Auth::user()->variable);
});
Route::get('/home/{variable}', 'HomeController@index');

然后 'variable' 的值将在您的 HomeController 索引方法中可用

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

https://laravel.com/docs/5.7/routing#route-parameters