简体   繁体   English

Laravel 4公共职能

[英]Laravel 4 Public Functions

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. 我创建了一个函数,说明如果'type'(我的数据库中的一列)等于五,它将显示其他人无法查看的按钮。 The problem is when I log out or log into a user that doesn't have type equals to five, it displays an error. 问题是当我注销或登录没有类型等于5的用户时,它会显示错误。 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');

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 . 在尝试访问User对象之前,请检查以确保用户通过使用手册中指定的Auth :: check()进行身份验证。

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

When a user is not authenticated, you have no access to ´Auth::user()´, so you need something like this: 如果用户未经过身份验证,则您无权访问'User :: 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');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM