douchuo1963 2017-10-28 05:52
浏览 65
已采纳

Laravel:在中间件中使用重定向时“尝试获取非对象的属性”[重复]

This question already has an answer here:

I recently change my authentication to Sentinel and everything seems to work beside the following problem: when the user is not authenticated, I would like to redirect him to the login page, but I always get an error "Trying to get property of non-object"

I have commented several standard middleware from the Kernel.php:

protected $middlewareGroups = [
    'web' => [
        // \Illuminate\Session\Middleware\AuthenticateSession::class,

protected $routeMiddleware = [
    // 'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    // 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    // 'can' => \Illuminate\Auth\Middleware\Authorize::class,
    // 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

And I have added a new middleware by myself:

<?php
namespace App\Http\Middleware;
use Closure;
use Sentinel;
class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if (Sentinel::check())
        {
            if (Sentinel::getUser()->roles()->first()->slug == 'admin' ||
                Sentinel::getUser()->roles()->first()->slug == 'superuser' )
            {
                return $next($request);
            }
        }
        else
        {
            return redirect('/login')
            ->with(['error' => "You do not have the permission to enter this site. Please login with correct user."]);
        }
    }
}

In case the user is logged in and I return the request, everything is working well. In case the user is not logged in or have a too low level, there will be an error triggered when the redirect is executed:

"Trying to get property of non-object"

/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php $response->headers->setCookie(

</div>
  • 写回答

1条回答 默认 最新

  • doutuo6048 2017-10-28 08:43
    关注

    Don't use an else in there! Otherwise, the case that Sentinel::check() is true but the user has not the right slugs isn't covered by any return value at all!.

    public function handle($request, Closure $next)
        {
            if (Sentinel::check())
            {
                if (Sentinel::getUser()->roles()->first()->slug == 'admin' ||
                    Sentinel::getUser()->roles()->first()->slug == 'superuser' )
                {
                    return $next($request);
                }
            }
    
            return redirect('/login')
                ->with(['error' => "You do not have the permission to enter this site. Please login with correct user."]);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退