Support the ongoing development of Laravel.io →
Configuration Packages Architecture

Following this thread I saw that a few weeks ago two new methods were added to Illuminate\Foundation\Http\kernel to conditionally append or prepend middleware at runtime.

My question is, where's the best moment/place, during a request lifecycle, to add a middleware?

Here's an example:

I'm trying to figure out where's the best place to add a call to prependMiddleware for a service provider that is only necessary for a development environment.

To be precise, I have a project with the clockwork package installed for debugging reasons. I've extracted the binding of the package's serviceProvider and I've placed it in the App\Providers\AppServiceProvider.

But to be honest, I'm struggling to find the correct place to add the next piece of code (or whatever is needed to accomplish that action...):

if (config('app.debug'))
{
    $kernel->prependMiddleware('Clockwork\Support\Laravel\ClockworkServiceProvider');
}

Any suggestion?

Last updated 3 years ago.
0

Also intrested in this

0

why don't you just put the middleware as needed in your app and put your app.debug conditional inside the middleware itself? in other words, add your ClockworkServiceProvider middleware to all routes or whichever routes it would normally be added to and then inside your middleware do this:

	public function handle($request, Closure $next)
	{
		if (config('app.debug'))
		{
			// do something
		}
		return $next($request);
	}
0

this would load your service provider and you could just insert it wherever you need in the kernel stack.

	public function handle($request, Closure $next)
	{
		if (config('app.debug'))
		{
			\App::register('Clockwork\Support\Laravel\ClockworkServiceProvider');
		}
		return $next($request);
	}
0

here's what i did for laravel 5.0.33. in the app\Http\Kernel.php.


    public function bootstrap() {

        parent::bootstrap(); // should have loaded environment detection now.

        if ($this->app->environment() == 'local') {
            $this->pushMiddleware('Clockwork\Support\Laravel\ClockworkMiddleware');
        }

    }

0

I use the AppServiceProvider I check the env and only load the service provider then I would not use the prependMiddleware method.

public function register()
    {
        $this->app->bind(
            'Illuminate\Contracts\Auth\Registrar',
            'App\Services\Registrar'
        );
        // if in dev mode then enable some dev tool service providers
        if ($this->app->environment() == 'local' or 'testing') {
            $this->app->register('Laracasts\Generators\GeneratorsServiceProvider');
            $this->app->register('Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider');


//            $loader = AliasLoader::getInstance();
            $this->app->register('Barryvdh\Debugbar\ServiceProvider');
            $loader = AliasLoader::getInstance();
            $loader->alias('Debugbar', 'Barryvdh\Debugbar\Facade');
//            $this->app->register('Clockwork\Support\Laravel\ClockworkServiceProvider');
//            $loader->alias('Clockwork', 'Clockwork\Support\Laravel\Facade');

        }

    }

0

Sign in to participate in this thread!

Eventy

Your banner here too?

jaumesala jaumesala Joined 20 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.