Laravel 4公共职能

时间:2013-08-02 20:35:21

标签: php laravel laravel-4

我有一个问题。我创建了一个函数,说明如果'type'(我的数据库中的一列)等于五,它将显示其他人无法查看的按钮。问题是当我注销或登录没有类型等于5的用户时,它会显示错误。我怎么能在一个函数中做到这一点?我尝试了各种各样的东西,但它总是显示错误。这是我的方法......

<?php

public function get_dash()
{
    $roles = Auth::user()->type;

    if ($roles == '5') {
        return View::make('admin.dash')->with('roles', $roles);
    } else {
        return Redirect::to('news/index')
            ->with('danger', 'You either have insufficient permissions 
                   to access this page or your user credentials 
                   are not refreshed.');
    }
}

我基本上想要这样,如果一个帐户中没有类型等于五,或者当我退出时它会正常加载......

return View::make('news/index');

2 个答案:

答案 0 :(得分:2)

在尝试访问User对象之前,请检查以确保用户通过使用manual中指定的Auth :: check()进行身份验证。

if (Auth::check())
{
    // The user is logged in...
}

答案 1 :(得分:0)

如果用户未经过身份验证,则您无权访问'Auth :: user()',因此您需要以下内容:

public function get_index()
{
    if( ! Auth::guest() ) 
    {
        $roles = Auth::user()->type;

        return View::make('aac.index')
                ->with('newss', News::all())
                ->with('roles', $roles);
    }
    else 
    {
        return Redirect::to('login');
    }
}