Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

Do I need to use "use Illuminate\Support\Facades\Route;" in web.php file?

I just installed the PHP Intelephense extension and noticed the entire file had errors of undefined type Route. I then found an old post here that says I should disable the undefined types check.

But, shouldn't there actually be the use state in the top of the web.php file?

use Illuminate\Support\Facades\Route;

But also, how does it work if currently there isn't this use statement?

*Laravel 6, code I'm working on but did not write at first

0 likes
6 replies
thinkverse's avatar

PHP Intelephense relies on static analysis to find and interact with types and classes. If you only have Route in a file without the use statement then Intelephense won't know what Route is.

It won't affect the application itself when running, PHP itself usually finds the correct classes. But it can break an application if no use statement is added, as it has done for me in the past. As a general rule, I always add the classes with a use statement. It keeps Intelephense happy and makes sure PHP uses the correct classes so, a win-win in my eyes.

1 like
Ligonsker's avatar

@thinkverse Thank you! I was wondering how it knows about Route if it didn't have use. Now I know PHP finds it. But I will add it nonetheless

Do you know how it works? How PHP finds it without use? Or it's also something that has to do with Laravel?

Snapey's avatar
Snapey
Best Answer
Level 122

@Ligonsker The routes file (eg web.php) is not a class, so it does not need its own use statements. It is imported by a class that does know what Route means. Here its just your IDE that you have to convince.

If you look in your RouteServiceProvider, this uses the file as a callback script;

    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

So you can imagine the contents of the web.php file being injected into this class, and using the Route import at the top of the Route Service Provider.

1 like
MohamedTammam's avatar

The routes are being registered through a method call in RouteServiceProvider which has that use line

use Illuminate\Support\Facades\Route;

That's why it's working without adding that line in web.php, because as I mentioned that file is being called under another file which includes it.

If it wasn't called that way, it wouldn't work because as PHP will not be able to know which class is that magically.

You have in error in your text editor because it doesn't know that, it just know that file is using a class that isn't defined in file.

1 like
Ligonsker's avatar

@mohamedtammam @snapey thank you guys! understood now. But would you still use the use statement? Or not?

Could it be a bad idea to actually use it when not necessary?

kokoshneta's avatar

@Ligonsker Adding a use statement at the top of your file has absolutely no negative impact or side effects (as long as you don’t use a different class called Route in the file, obviously), so go ahead and add it.

Many IDEs allow you to add use statements automatically when you type out class names for this exact reason.

1 like

Please or to participate in this conversation.