我看到了这个问题。我有类似的问题,但它不起作用。 Laravel 4 public functions
我有一个视图编辑器,其中包含基本布局中的一些代码。这是我的作曲家:
View::composer(array('common.menu_addition','common.base_errors','common.view_special'), function($view)
{
if(Auth::check()) {
$roles = Auth::user()->type;
if ($roles == '5') {
$view->with('roles', $roles);
} else {
return Redirect::to('news/index');
}
}
});
当我没有登录时,它完美无缺。但我的一个视图作曲家文件是这样的:
<div class="pull-right">
@if (Auth::check())
@if ($roles == 5)
<ul class="nav">
<li>
<a href="{{ URL::to('admin/dash') }}">
<i class="icon-eye-open">
</i>
<strong>
Admin Dashboard
</strong>
</a>
</li>
</ul>
@endif
@endif
</div>
登录时,不会显示任何网站。它只是在该视图作曲家文件中说Undefined variable: roles
。
答案 0 :(得分:0)
我认为在视图编辑器中可以返回这样的重定向。即使你可以把这个逻辑放在一个不好的地方。如果用户没有正确的身份验证级别,您应该创建一个过滤器http://four.laravel.com/docs/routing并重定向。
答案 1 :(得分:0)
看起来你没有事先定义$ roles而没有将$ roles传递给你的视图。
当您对视图中的$ roles进行检查时,您需要确保该变量实际存在。其他可能性是将支票更改为
@if (isset($roles) && $roles == 5)
忽略错误但从长远来看会变得非常混乱。
因此,请务必事先在视图编辑器和其他任何位置初始化视图变量。对于非初始化变量,Laravel试图比PHP更加严格。