laravel-5 - 流明中的“auth”中间件抛出错误

标签 laravel-5 laravel-routing lumen laravel-middleware

我是第一次测试流明。试用 auth 中间件会引发错误。我想知道这样的中间件是随 lumen 一起提供的还是我们需要自己实现? 这是我的路线文件

$app->group(['middleware' => 'auth'], function ($app) {

    $app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});

这是尝试访问路由时的错误

ErrorException in Manager.php line 137:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Auth\Guard' does not have a method 'handle'

最佳答案

如您所见,Lumen 容器中的 auth 绑定(bind)到 Illuminate\Support\Manager\AuthManager。所以,是的,您必须创建自己的中间件。这是您的案例的示例。

app/Http/Middleware中制作自己的中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;

class Authenticate
{
    /**
     * 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->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                // Lumen has no Redirector::guest(), this line is put the intended URL to a session like Redirector::guest() does
                app('session')->put('url.intended', app('url')->full());
                // Set your login URL here
                return redirect()->to('auth/login', 302);
            }
        }

        return $next($request);
    }
}

在此之后,将您的中间件绑定(bind)到容器中。您可以在 bootstrap/app.php 中执行此操作。在 return 之前添加这两行。

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../app/Http/routes.php';
});

$app->bind('App\Http\Middleware\Authenticate', 'App\Http\Middleware\Authenticate');
$app->alias('App\Http\Middleware\Authenticate', 'middleware.auth');

现在,不要在中间件中使用 auth,而是使用 middleware.auth:

$app->group(['middleware' => 'middleware.auth'], function ($app) {
    $app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});

关于laravel-5 - 流明中的“auth”中间件抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31564850/

相关文章:

php - 在 Lumen 中执行迁移时未设置表名

php - REST API 请求参数验证 Laravel

php - 将硬编码值从路由传递到 Controller (Laravel)的正确方法?

Laravel - 获取模型中的路由参数

php - 如何在 laravel 中的一个函数中为同一路由返回多个 View

php - 如何在 PHP Laravel Lumen 中建立与数据库的连接?

laravel - 测试 assertDispatched 仅在使用事件助手时有效

php - Laravel:在分页中自定义页面链接

php - 如果密码字段为空,则编辑用户信息而不更改密码,如果我们填写匹配确认,则编辑它

php - Controller 中的流明(laravel)翻译