I'm having a hard time trying to configure my routes using a config/routes.php
file in Symfony 5.1.
As per the Symfony routing documentation, I should be able to configure my routes in a PHP file:
Instead of defining routes in the controller classes, you can define them in a separate YAML, XML or PHP file. The main advantage is that they don't require any extra dependency.
But in practice, Symfony only recognizes the routes if I put my routes in a file routes.yaml
.
Routes configured inside a file routes.php result in the error "No route found for "GET /something" (404 Not Found)". When running debug:router
, these routes are not listed.
The same route works great when configured in routes.yaml
.
In a different project using Symfony 5.0.8
, route configuration via routes.php
is working like a charm.
This is how I tested it:
Created a controller (omitted, since it's no relevant, any controller would do)
Created a
routes.php
file:
//config/routes.php example
use App\Controller;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return function(RoutingConfigurator $routes)
{
$routes->add('schools_list', '/schools')
->controller([Controller\SchoolController::class, 'list'])
->methods(['GET']);
};
- Running
debug:router
will result in:
---------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
---------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
---------------- -------- -------- ------ --------------------------
- Configured the same route inside
routes.yaml
:
#config/routes.yaml
schools_list:
path: /schools
controller: App\Controller\SchoolController::list
methods: GET
- Running
debug:router
will result in:
---------------- -------- -------- ------ --------------------------
Name Method Scheme Host Path
---------------- -------- -------- ------ --------------------------
_preview_error ANY ANY ANY /_error/{code}.{_format}
schools_list GET ANY ANY /schools
---------------- -------- -------- ------ --------------------------