2013-08-02 87 views
0

我有問題。我做了一個函數,說如果'type'(我的數據庫中的一列)等於五,它將顯示其他人無法查看的按鈕。問題是當我註銷或登錄到沒有類型等於五的用戶時,它顯示一個錯誤。我怎麼能在一個if函數中做到這一點?我嘗試了各種各樣的東西,但它總是顯示錯誤。這是我的公共職能:Laravel 4公共職能

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');)。

+0

顯示什麼錯誤? –

+0

@Antonio Carlos Ribeiro 它說錯誤在$ roles = Auth :: user() - > type; ,當我將它作爲客人查看時,錯誤:'嘗試獲取非對象的屬性' – dinomuharemagic

回答

2

之前試圖訪問用戶對象,檢查以確保用戶通過使用Auth :: check()進行身份驗證,如manual中所述。

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

當用戶沒有通過驗證,你有()沒有進入'Auth ::用戶,那麼你需要的東西是這樣的:

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'); 
    } 
} 
+0

對不起,我有兩個幾乎相同的代碼。這個被放置在MainController中: http://paste.laravel.com/FZg 我已經編輯了基於你的答案,它說:'這個網頁有一個重定向循環 在http:// localhost的網頁:8000/news/index導致了太多的重定向。 '我希望客人查看索引,但是隻能在管理員儀表板訪問管理員儀表板時,他們的'類型'等於5. – dinomuharemagic

+0

它總是重定向到同一頁面,所以重定向到登錄,或者只是渲染一個視圖您的用戶登錄是強制性的...剛剛編輯。 –

+0

沒關係,搞定了。無論如何,謝謝你使用了Auth :: check()。 – dinomuharemagic