Laravel 5 ';auth';流明抛出错误中的中间件
我第一次测试流明。尝试Laravel 5 ';auth';流明抛出错误中的中间件,laravel-5,laravel-routing,lumen,laravel-middleware,Laravel 5,Laravel Routing,Lumen,Laravel Middleware,我第一次测试流明。尝试auth中间件会抛出错误。我想知道这样的中间件是随lumen一起提供的,还是我们需要实现自己的中间件? 这是我的文件 $app->group(['middleware' => 'auth'], function ($app) { $app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']); }); 这就是尝试访问路由时的错误 ErrorException in M
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'
正如您所看到的,
auth
在绑定到illumb\Support\Manager\AuthManager
的Lumen容器中。所以,是的,您必须创建自己的中间件。这是你的例子
在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);
}
}
现在,不要在中间件中使用auth
,而是使用middleware.auth
:
$app->group(['middleware' => 'middleware.auth'], function ($app) {
$app->get('/', ['as' => 'api', 'uses' => 'ApiController@index']);
});
实际上,您只需在
bootstrap/app.php
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
你发布的代码与问题无关我想这是真的。但是我认为包含了
auth
中间件,但它抛出了错误。我认为Lumen没有auth中间件。除非你手动添加这个?
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);