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
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.
@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.
@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.