3
votes

This is probably nitpicky, but I was wondering how the namespace for the Laravel 4.2 "Route" works (using the resource function).

So... in all the documentation (http://laravel.com/docs/4.2/controllers#resource-controllers), you see this:

Route::resource('photo', 'PhotoController');

However, when I go to the "Route" documentation (http://laravel.com/api/4.2/Illuminate/Routing/Route.html) you can see that the function "resource", doesn't exist there. Instead, it exists under "Router" (http://laravel.com/api/4.2/Illuminate/Routing/Router.html).

How do I use the PHP "use" statements to specify that I'm using the resource function? The example itself is accurate and works when I call (in that it shows all the routes that I expect):

php artisan routes

How does Laravel make the Route vs. Router namespace work?

3

3 Answers

3
votes

Laravel heavily uses Facades. They're basically classes that allow static access (Route::resource()) to methods that are not static at all. In the background it makes a new instance of the Router class and then calls the method resource(). You can find more information about it in the docs

If you are looking for proper auto-completion and other IDE features that rely on resolving the class, try the laravel ide helper

1
votes

The thing is that Route here is not class but it's a facade. If you look at Facade class reference you will see that Route facade is using Illuminate\Routing\Router router so it's the class you want

1
votes

The Route in app/routes.php is just another of Laravel's various facades and contains rules processed by the Laravel Routing Engine to produce the actual routes that will be used in your application.

To get a better understanding, I'd suggest checking out this short guide to Laravel Architecture to understand how requests are received and processed in the framework, as well as Rebuilding Laravel which explains how the various Laravel components are built starting from the first file that is processed.