Skip to content

Routes not being loaded from config/routes.php #37084

@carlosdiasmarques

Description

@carlosdiasmarques

Symfony version(s) affected: 5.1.0

Description

Routes configured inside a file routes.php are not recognized, resulting 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 works perfectly.

I don't know if this is really a bug or a case of outdated documentation. The change was made in this commit, that makes the method configureRoutes rely only in .yaml files.

This following commit makes it possible to use .php files again, but this change was reverted.

How to reproduce

  1. Create a controller:
<?php
//src/Controller/SchoolController.php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;

class SchoolController
{
    public function list()
    {
        return new Response('ok');
    }
}
  1. Create a routes.php file:
<?php
//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']);
};
  1. Running debug:router will result in:
 ---------------- -------- -------- ------ -------------------------- 
  Name             Method   Scheme   Host   Path                      
 ---------------- -------- -------- ------ -------------------------- 
  _preview_error   ANY      ANY      ANY    /_error/{code}.{_format}  
 ---------------- -------- -------- ------ -------------------------- 
  1. Configure the same route inside routes.yaml:
#config/routes.yaml
schools_list:
    path: /schools
    controller: App\Controller\SchoolController::list
    methods: GET
  1. 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                  
 ---------------- -------- -------- ------ -------------------------- 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy