Majeed's avatar

Route Facade vs $router variable

In Laravel we use the Route facade for declare a route. for example:

Route::get('/',function(){

   return view('welcome');

});

in the other hand, In Lumen we use the $router instead of Route facade. for example:

$router->get('/',function(){

   return $router->app->version();

});

my question is where this $router variable is declare or bind in lumen. can i use this in Laravel Project ?

0 likes
3 replies
Nakov's avatar

In Lumen if you open bootstrap/app.php that's where the $router is being passed to the web.php script. It is being resolved from the IoC container. In Laravel RouteServiceProvider is the one where the routes are loaded.

mstrauss's avatar
mstrauss
Best Answer
Level 14

It looks like in Lumen, they make the $router variable publicly available here: https://github.com/laravel/lumen-framework/blob/0f450f8d6ee47bdef6c88a2492883c6e48a33a95/src/Application.php#L82


    /**
     * The Router instance.
     *
     * @var \Laravel\Lumen\Routing\Router
     */
    public $router;

Although the Route facade seems to also be available in Lumen: https://github.com/laravel/lumen-framework/blob/0f450f8d6ee47bdef6c88a2492883c6e48a33a95/src/Application.php#L703

Whereas Laravel uses the Route facade, which references the same router as Lumen: https://laravel.com/docs/5.8/facades#facade-class-reference. And I would guess that you can use the $router variable in your Laravel project, but you will have to declare the binding yourself as it is not made public by Laravel (that I am aware of anyway). Functionally it should work the same though, so it would just be a convention thing. And most Laravel developers, if anyone else is working on the project with you or will be in the future, are used to the Route facade.

Nakov's avatar

in Laravel it used to be the same when routes.php file was used to define your routes, now as they separated the routes in web.php and api.php for convenience the facade is used.

2 likes

Please or to participate in this conversation.