dqvzfp6468 2015-06-19 17:33
浏览 77

在PHP Laravel 5中配置路由限制的最佳方法是什么?

I'm new to Laravel 5 middleware - and I just recently added a new user type == Dev into my application. I need to set a certain route restriction for that user type - in term of what they can/can't access.


So I create

DevMiddleware.php

<?php

namespace App\Http\Middleware;

use App\Article;
use Closure, View ;
use Illuminate\Contracts\Auth\Guard;

class DevMiddleware
{
    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($this->auth->getUser()->type !== "Dev") {
            return View::make('errors.404');
        }

        return $next($request);
    }
}

I registered it in my

Kernel.php

'dev' => 'App\Http\Middleware\DevMiddleware',


I use it in my

Routes.php

$router->group(['middleware' => ['auth', 'admin', 'dev' ] ], function() {

    //Any routes goes in here

});

When I go those route in there - keep getting 404 error.

I want to be able to go to those routes.

What did I do wrong here ? How do I fix it ?

  • 写回答

2条回答 默认 最新

  • 普通网友 2015-06-19 23:05
    关注

    Are you using the same logic for the other middleware? If so then the logic would act like an OR statement as in if 'auth' fails, or 'admin' fails, or 'dev' fails, then 404.

    I am not sure of the "best" way to handle what you want but I have an example from an open source project; laravel-5-boilerplate:

    Route::group([
            'middleware' => 'access.routeNeedsRole',
            'role'       => ['admin', 'dev'],
            'redirect'   => '/',
            'with'       => ['flash_danger', 'You do not have access to do that.']
        ], function ()
        {
        //Any routes goes in here
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化