如何在Laravel中编辑dingo / api auth中间件?

时间:2018-05-29 08:46:46

标签: php laravel dingo-api

在一个完全宁静的Laravel项目中,我正在使用examples of what can be done through configuration包。在处理任何请求之前,我需要设置一些与当前用户相关的配置和其他内容。当我们使用dingo时,我们可以像这样访问当前用户:

api.auth

首先,我认为我应该在服务提供商中这样做。但是在那里,Laravel尚未启动野狗认证,因此它给我一个错误。然后我想我需要编辑名为<?php $api = app('Dingo\Api\Routing\Router'); $api->version('v1', ['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function ($api) { $api->group(['prefix' => 'admin', 'middleware' => 'api.auth'], function ($api) { $api->get('checkRole/{branch_id}', 'RoleController@getRoles'); 的dingo auth中间件来执行此操作。它在我的路线上的用法是这样的:

{{1}}

但是我没有任何访问权限,因为它是dingo包中的内置中间件。那么在这种情况下我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,但是我忘了在这里回答。 我只是扩展了digo auth中间件并覆盖了其handle方法。但是我必须通过项目中的所有路由文件替换中间件名称(我正在使用nwidart/laravel-modules包来编写模块化项目)。

<?php

    namespace App\Http\Middleware;

    use Closure;
    use Dingo\Api\Http\Middleware\Auth;

    class ApiAuth extends Auth
    {
        public function handle($request, Closure $next)
        {
            $route = $this->router->getCurrentRoute();

            if (!$this->auth->check(false))
                $this->auth->authenticate($route->getAuthenticationProviders());

            ...
            ...

            return $next($request);
        }
    }