所以我的 routes.php 中有一个变量,我想将它传递给route :: get,这是我的代码以获取更多详细信息:
$user = User::find(Auth::user()->username);
Route::get('/services/here i want to put my variable', 'ServiceController@show');
所以,如果有人知道我会非常感激
答案 0 :(得分:1)
使用以下内容来改变路线的问题:
$user = User::find(Auth::user()->username);
Route::get("/services/$user->username", 'ServiceController@show');
在用户不存在的情况下,您是否可能会遇到一些问题。如果您这样做并转到命令行并执行:
php artisan routes
将不会记录用户,以便将路线指向/services/
。您需要做的就是创建一个过滤器并在其上处理您的路线:
路线:
Route::get("/services/{username}", array('before' => 'user-must-be-logged-in', 'uses' => 'ServiceController@show'));
对其进行一些检查的过滤器:
Route::filter('user-must-be-logged-in', function()
{
if ( ! Auth::check())
{
App::abort(404); /// user is not logged in, this route does not exist
}
if ( ! User::where('username', $username)->first() )
{
App::abort(404); /// user does not exists, this route does not exist
}
if (User::where('username', $username)->first()->id !== Auth::user()->id)
{
App::abort(404); /// selected is not the current logged user, this route does not exist
}
});
控制器用它做任何你需要的事情:
class ServiceController extends Controller {
public function show()
{
$user = User::where('username', $username)->first();
// now do whatever you need with your user;
}
}
答案 1 :(得分:0)
假设你想要放在那里的$ user
$user = User::find(Auth::user()->username);
Route::get("/services/$user", 'ServiceController@show');
交换单引号用于双打并传入你的var(或者用你的单引号删除连接。你的var