Laravel Blade 不适用于哨兵方法


Laravel Blade not working with Sentry method

我正在尝试编写一个 .blade.php 视图,它确实:

  • 显示用户配置文件信息
  • 如果用户被禁止,请改为显示消息"用户已被禁止"。
  • 如果您
  • 以管理员身份登录(即,如果您具有"admin"权限),则不要显示该消息,而是显示用户配置文件,其中包含额外的信息,说明用户已被禁止。

    以下是我的观点:

    @extends ('layout.main')
    @section('content')
    @if(! $throttle->banned==1)
            {{ $user->username }} <br>
            {{ $user->summoner_name }}<br><br>
            {{ $user->bio }}<br>
            {{ $user->punishments }}<br>
    @else
            {{ "This user is banned." }}
                    @if(Sentry::getUser()->hasAccess('admin'))
                            {{ $user->username }} <br>
                            {{ $user->summoner_name }}<br><br>
                            {{ $user->bio }}<br>
                            {{ $user->punishments }}<br>
                            {{ ($throttle->banned == 1) ? "User is banned." : '' }}<br>
                            {{ $throttle->getSuspensionTime() }}<br>
                     @endif
    @endif
    @stop
    

还有我的控制器:

<?php
class ProfileController extends BaseController {
        public function main($username) {
                try {
                        $throttle = Sentry::findThrottlerByUserLogin($username);
                        $user = User::where('username','=',$username);
                        if($user->count()) {
                                $user = $user->first();
                                return View::make('profile.main')
                                        ->with('user',$user)
                                        ->with('throttle',$throttle);
                        } else {
                                return App::abort(404);
                        }
                }
                 catch (Cartalyst'Sentry'Users'UserNotFoundException $e)
                 {
                        echo 'User not found.';
                 }
        }
}

也就是说,每当我使用

@if(Sentry::getUser()->hasAccess('admin'))

语法对我来说看起来不错,所以我真的不明白为什么 Sentry 不将其识别为对象。会是什么?有没有简单的解决方案?

提前感谢!

如果用户

被禁止,Sentry::getUser()将返回null。因此,您正在对 null 对象调用 hasAccess(),从而给出您看到的错误。

我假设您的User模型正在扩展Cartalyst'Sentry'Users'Eloquent'User模型,所以我相信您可以在视图中使用$user->hasAccess('admin')

其他一些注意事项:

我认为您可以将控制器缩短为如下所示:

try {
    // throws Cartalyst'Sentry'Users'UserNotFoundException if user not found
    $throttle = Sentry::findThrottlerByUserLogin($username);
    // throws Cartalyst'Sentry'Users'UserNotFoundException if user not found
    $user = Sentry::findUserByLogin($username);
    return View::make('profile.main')
        ->with('user',$user)
        ->with('throttle',$throttle);
}
catch (Cartalyst'Sentry'Users'UserNotFoundException $e)
{
    return App::abort(404);
}

此外,您应该使用$throttle->isBanned()而不是检查$throttle->banned == 1。 同样适用于$throttle->isSuspended(),如果您需要的话。

好的,问题解决了。

@patricus,感谢您的帮助!这不是我的意思,但它给了我需要的信息。当我调用该方法Sentry::getUser()时,我试图获取当前登录并查看视图的用户,而不是配置文件所有者。但正如您所说,该方法返回 NULL,因为我在没有检查是否有登录用户的情况下使用它!

所以真正解决问题的是重写

@if(Sentry::getUser()->hasAccess('admin'))

@if(Sentry::check() && Sentry::getUser()->hasAccess('admin'))

并感谢您的额外提示。我对Sentry,Laravel和PHP本身相当陌生,所以代码可能会变得混乱。全部固定和清洁。

谢谢!