pushMiddlewareToGroup package development bug in Laravel 10

Janez Cergolj

This is a heads up post about a pushMiddlewareToGroup bug in #laravel package development and how to fix it.

While working on one of my packages (save last index or show route and redirect back after store/update/delete)[https://github.com/jcergolj/after-action-redirect-pagination-url-for-laravel] I encountered pushMiddlewareToGroup bug: middleware wasn't added to the web group.

The Original Code with the Bug:

<?php

namespace Jcergolj\AfterActionRedirectUrlForLaravel;

use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;

class AfterActionRedirectUrlForLaravelServiceProvider extends ServiceProvider
{
    public function boot(Router $router)
    {
        $router->pushMiddlewareToGroup(
            'web',
            \App\Http\Middleware\SetIntendedUrlMiddleware::class
        );
    }
}

Fix

<?php

namespace Jcergolj\AfterActionRedirectUrlForLaravel;

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\ServiceProvider;

class AfterActionRedirectUrlForLaravelServiceProvider extends ServiceProvider
{
    public function boot(Kernel $kernel)
    {
        $kernel->appendMiddlewareToGroup(
            'web',
            \App\Http\Middleware\SetIntendedUrlMiddleware::class
        );
    }
}

Notice the line appendMiddlewareToGroup('web', \App\Http\Middleware\SetIntendedUrlMiddleware::class);. This small change made all the difference. I'm not exactly sure why the original version didn't work, but this solution does the trick!