Ad

Laravel 5 Cashier Middleware Routing Error

I've implemented the cashier / billing feature from Laravel 5 and I'm trying to protect a group of routes using middleware which checks for a subscription.

I'm getting the following error:

Argument 2 passed to App\Http\Middleware\HasSubscription::handle() must be an instance of App\Http\Middleware\Closure, instance of Closure given

Heres my Middleware

<?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);
    }
}

Heres my protected route

Route::get('home', '[email protected]')->middleware('subscription');

Heres my applications route declaration

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,
    ];

Any idea why I get the error at the top?

Ad

Answer

Just add

use Closure;

to the top of your middleware, just before class definition:

namespace App\Http\Middleware;

use Closure;

class HasSubscription
{
    ...

Take a look on the the example in manual: http://laravel.com/docs/5.1/middleware#defining-middleware

Ad
source: stackoverflow.com
Ad