Search code examples
phplaravellumen

Where is the '$router' variable defined in 'web.php' in Lumen?


Can't find where the $router variable is defined in Lumen web.php. The content of web.php is just like this and it is not clear how the $router variable is defined:

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
    return $router->app->version();
});

As you can see there is no pre-definition of the $router variable. Is it loaded here from another file?


Solution

  • This is magic :) if you open bootstrap/app.php which is what bootstraps the whole application and loads the routes you will see this code:

    $app->router->group([
        'namespace' => 'App\Http\Controllers',
    ], function ($router) {
        require __DIR__.'/../routes/web.php';
    });
    

    So the $router is available in the web.php file as a global property.