When building a subscription based application, you will commonly need to restrict access to certain routes to users that have an active subscription. For example, you may not want to let a user create a project if they are not subscribed to a billing plan. For that reason, Spark provides a convenient subscription verification middleware that you may assign to your application's routes.

If your application uses Laravel 11's streamlined application structure which configures middleware within the bootstrap/app.php file, then Spark's subscription verification middleware alias is automatically registered for you internally. However, if you are using an application structure that does not utilize the bootstrap/app.php file for middleware configuration, you may need to manually register the subscribed middleware alias in your application's App\Http\Kernel class:

use Spark\Http\Middleware\VerifyBillableIsSubscribed;

protected $middlewareAliases = [
    // ...

    'subscribed' => VerifyBillableIsSubscribed::class

Then, you may attach the subscribed middleware to any of your application's route definitions:

Route::post('/projects', [ProjectController::class, 'store'])
        ->middleware(['auth', 'subscribed']);

If the user has an active subscription, the request will continue to execute normally. However, if the user does not have an active subscription, they will be redirected to your application's Spark billing portal. If the request is an XHR request, a response with a 402 HTTP status code will be returned to the client.

Manually Inspecting Subscription States

Of course, you may always manually inspect a billable model's subscription status using the methods provided by Laravel Cashier, which can be especially useful for verifying that a user is subscribed to a particular plan.