How can I check if the app is using the ConvertEmptyStringsToNull Middleware?

Published 1 week ago by malhayek

I am writing a Laravel console-command. When the command is called using the command-line, I need to know if the ConvertEmptyStringsToNull is a registered middlewear in the Application or not.

When I looked at Illuminate\Foundation\Http\Kernel (https://laravel.com/api/5.1/Illuminate/Foundation/Http/Kernel.html) I can see that it has a public method called hasMiddleware that should give me the answer I am looking for.

However, I am not sure what is the correct way to create a new of the App\Http\Kernel which expends the Illuminate\Foundation\Http\Kernel class. More, I am not sure if there is a better way to get this answer beside creating a new instance of the Kernel.

My command class extends the Illuminate\Console\Command like this

use Illuminate\Console\Command;

class TestCommand extends Command 
{
    public function handle()
    {
        $router = new \Illuminate\Routing\Router(); // this is not working!
        $kernel = new \App\Http\Kernel($this->getLaravel(), $router);  // $this->getLaravel() is a method that the Illuminate\Console\Command has which return the instance of the application
    }
}

Is there a better way to check if the app has the ConvertEmptyStringsToNull middleware beside creating a new instance of the Kernel class? Is there a way to access the Kernel class without creating a new instance? If not, what is the proper way to create a new instance of the Kernel class?

Best Answer (As Selected By malhayek)
spodlogar

This is how you can do that

app()->make(App\Http\Kernel::class)->hasMiddleware($middleware)
jlrdw
jlrdw
1 week ago (191,780 XP)

If you haven't commented it out it is.

malhayek

@jlrdw I am not following you. What do you mean?

spodlogar

in the app/Http/Kernel.php

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, // BOOM
    \App\Http\Middleware\TrustProxies::class
];
malhayek

@spodlogar, I know that :) but in order for me to do a real-time "when the command is called" check to see if the middleware is registred, I would need to create a instances of the Kernel class then possible call the hasMiddleware method to get the answer.

My question is how can I create an instance of this class

spodlogar

I see what you are saying

spodlogar

You need an instance of App\Http\Kernel to call the hasMiddleware method on?

spodlogar

This is how you can do that

app()->make(App\Http\Kernel::class)->hasMiddleware($middleware)
malhayek

@spodlogar That is exactly what I need :)

Does the make() method creates a new instance each time or does it create a new one if one does not already exists?

Also, is make() behaves the same on Laravel 5.1+?

spodlogar

I believe it creates one if there is not one and resolves it if there is one already.

It should behave the same.

Sign In or create a forum account to participate in this discussion.