1.1 Usually Laravel will load route at:
routes/web.php
routes/api.php
1.2 You can cache routes and load them to increase performance as below
xxx@MINGW64 /d/Projects/laravel70
$ php artisan route:cache
Route cache cleared!
Routes cached successfully!
1.3 Routes-v7 cache file is generated
bootstrap/cache/routes-v7.php
app('router')->setCompiledRoutes(
array (
'compiled' =>
array (
0 => false,
1 => array (
'/api/user' => array (
0 => array (
0 => array (
'_route' => 'generated::2jAD1CNWGE16QUvS',
),
1 => NULL,
2 => array (
'GET' => 0,
'HEAD' => 1,
),
3 => NULL,
4 => false,
5 => false,
6 => NULL,
),
),
'/login' => array (
0 => array (
0 => array (
'_route' => 'generated::VZkR4pvCtKavuvoA',
),
1 => NULL,
2 => array (
'GET' => 0,
'HEAD' => 1,
),
3 => NULL,
4 => false,
5 => false,
6 => NULL,
),
),
),
'attributes' => array ()
)
2.1 Read routes from provider
app/Providers/RouteServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
public function boot()
{
parent::boot();
}
}
2.2 Boot RouteServiceProvider of laravel Foundation
vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php
<?php namespace Illuminate\Foundation\Support\Providers;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Traits\ForwardsCalls;
class RouteServiceProvider extends ServiceProvider
{
use ForwardsCalls;
protected $namespace;
public function boot()
{
$this->setRootControllerNamespace();
if ($this->routesAreCached()) {
$this->loadCachedRoutes();
} else {
$this->loadRoutes();
$this->app->booted(function () {
$this->app['router']
->getRoutes()->refreshNameLookups();
$this->app['router']->getRoutes()
->refreshActionLookups();
});
}
}
protected function loadCachedRoutes()
{
$this->app->booted(function () {
require $this->app->getCachedRoutesPath();
});
}
protected function loadRoutes()
{
if (method_exists($this, 'map')) {
$this->app->call([$this, 'map']);
}
}
}
2.3 Application
With
$this->app = vendor/laravel/framework/src/Illuminate/Foundation/Application.php
2.3.1 Application read route from cache
public function getCachedRoutesPath()
{
return $this->normalizeCachePath('APP_ROUTES_CACHE',
'cache/routes-v7.php');
}
2.3.2 Application read route from: routes/web.php|api.php
app/Providers/RouteServiceProvider.php
<?php namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
}
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
3.1 When you clear routes cache
xxx@MINGW64 /d/Projects/laravel70
$ php artisan route:clear
Route cache cleared!
3.2 Routes are get from
->group(base_path('routes/web.php'));
->group(base_path('routes/api.php'));