Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel & Namespacing

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?

like image 478
theamycode Avatar asked Nov 19 '25 03:11

theamycode


2 Answers

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

like image 182
lukasgeiter Avatar answered Nov 20 '25 19:11

lukasgeiter


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

like image 29
Marcin Nabiałek Avatar answered Nov 20 '25 17:11

Marcin Nabiałek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!