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

davestead's avatar

Why is a function necessary in Route::get?

I'm watching Laracasts, Laravel from Scratch, and the narrator shows the call in routes/web.php, as:

Route::get('/', function () { return view('welcome'); });

What I'm not understanding is why a function was determined to be the best thing to be in the 2nd param, as opposed to just specifying a view string.

My PHPStorm editor can't find the get function, or the Route class for some reason when I hover over or Ctrl+click them.

0 likes
4 replies
zak's avatar

That is an anonymous function, and you can do whatever you'd like in there. The reason it's like that by default is because it may be a handy starting point to test stuff out.

Laravel also has a view() method on the Route facade, so you can clean it up by doing something like this:

// Register a GET endpoint at '/' that returns the 'welcome' view
Route::view('/', 'welcome');

It is also worth mentioning that most IDEs will not be able to index Laravel methods and classes, simply because the framework resorts to a lot of magic methods. I do however believe there are Laravel plugins that you can get for PHPStorm that will let you do stuff like this.

Talinon's avatar

I think what you are referring to is a route closure that ships out-of-the-box. I think he's just illustrating a basic concept at that point within the series. Keep watching, he'll certainly follow up with best practices.

Please or to participate in this conversation.