Laravel 4公共职能 [英] Laravel 4 Public Functions

查看:60
本文介绍了Laravel 4公共职能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我做了一个函数,说类型"(数据库中的一列)等于5,它将显示其他人无法查看的按钮.问题是当我注销或登录到类型不等于5的用户时,它会显示错误.我该如何在函数中执行此操作?我尝试了各种方法,但始终显示错误.这是我的方法...

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

推荐答案

在尝试访问User对象之前,请使用手册.

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...
}

这篇关于Laravel 4公共职能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆