我已经实施了Laravel 5的收银台/计费功能,并且我尝试使用检查订阅的中间件来保护一组路由。
我收到以下错误:
Argument 2 passed to App\Http\Middleware\HasSubscription::handle() must be an instance of App\Http\Middleware\Closure, instance of Closure given
继承我的中间件
<?php
namespace App\Http\Middleware;
class HasSubscription
{
public function handle($request, Closure $next)
{
if ($request->user() && ! $request->user()->subscribed()) {
// This user is not a paying customer...
return redirect('subscription');
}
return $next($request);
}
}
继承我受保护的路线
Route::get('home', 'PagesController@index')->middleware('subscription');
继承我的申请路线声明
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'subscription' => \App\Http\Middleware\HasSubscription::class,
];
我知道为什么我会在顶部出现错误?
答案 0 :(得分:1)
添加
use Closure;
到中间件的顶部,就在课程定义之前:
namespace App\Http\Middleware;
use Closure;
class HasSubscription
{
...
查看手册中的示例:http://laravel.com/docs/5.1/middleware#defining-middleware