Php laravel 4视图::共享不起作用
我想在用户登录后通过我的所有视图共享用户名。我在imy routes.php中直接尝试了这一点:Php laravel 4视图::共享不起作用,php,authentication,view,laravel,share,Php,Authentication,View,Laravel,Share,我想在用户登录后通过我的所有视图共享用户名。我在imy routes.php中直接尝试了这一点: View::share('user', $user); 但它没有分享任何东西。我也试着把它放在下面的路线上: Route::post('auth', function() { $user = Input::get('username'); View::share('user', $user); $credentials = array( 'username'
View::share('user', $user);
但它没有分享任何东西。我也试着把它放在下面的路线上:
Route::post('auth', function()
{
$user = Input::get('username');
View::share('user', $user);
$credentials = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if (Auth::attempt($credentials))
{
//Cache::put('user', $user);
return View::make('panel')->with('user', $user);
}
//return 'user :' .$user. ' and pass : ' .$pass;
return Redirect::to('/');
});
你能帮我一下吗
编辑:
我解决了在我需要的每一把刀片上使用这个问题:
$user = Auth::user()->username;
但是我想为我的所有视图定义一次-有人能给我一个提示吗?您可以创建一个路由过滤器,它将用户名共享给视图。现在,您可以将此过滤器应用于每个管线或管线组,以便在视图中使用它
Route::filter('shared.user', function()
{
$user = '';
if(Auth::check())
{
$user = Auth::user()->username;
}
View::share('user', $user);
});
Route::get('hello', array('before' => 'shared.user', function()
{
return View::make('hello');
}));
Route::group(array('before' => 'shared.user'), function()
{
// routes which show the username
});
您是否将此用户名放置在每个页面的不同位置?如果不是,请在主布局中执行一次。即使你是,你不能抓取变量并在你的主布局中分配它吗?你正在使用布局,对吗?