Laravel 4 Public Functions

会有一股神秘感。 提交于 2020-01-17 06:38:30

问题


I've got a question. I made a function that says if 'type' (a column in my database) is equal to five, it'll display buttons that others can't view. The problem is when I log out or log into a user that doesn't have type equals to five, it displays an error. How could I do this in a function? I tried various things, but it always displays errors. Here's my method...

<?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.');
    }
}

I basically want it so that if no type equals to five in an account, or when I log out it'll load normally...

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

回答1:


Before trying to access the User object, check to make sure that the user is in fact authenticated by using Auth::check() as specified in the manual.

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



回答2:


When a user is not authenticated, you have no access to ´Auth::user()´, so you need something like this:

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


来源:https://stackoverflow.com/questions/18025701/laravel-4-public-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!