A
A
Alexander Vladimirovich2019-02-22 08:19:31
symfony
Alexander Vladimirovich, 2019-02-22 08:19:31

How to set the priority of routes (routing)?

Greetings!
I am making a website with page addresses and pages stored in a database.
Following the example https://symfony.com/doc/current/routing/slash_in_p... I made a controller

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class TextPageController extends AbstractController
{
    /**
     * @Route("/{token}", name="text_page", requirements={"token"=".+"})
     */
    public function index()
    {
        // ...
    }
}

php bin/console debug:router
 -------------------------- -------- -------- ------ -----------------------------------
  Name                       Method   Scheme   Host   Path
 -------------------------- -------- -------- ------ -----------------------------------
  app_login                  ANY      ANY      ANY    /login
  text_page                  ANY      ANY      ANY    /{token}
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}
  _wdt                       ANY      ANY      ANY    /_wdt/{token}
  _profiler_home             ANY      ANY      ANY    /_profiler/
  _profiler_search           ANY      ANY      ANY    /_profiler/search
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open
  _profiler                  ANY      ANY      ANY    /_profiler/{token}
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
 -------------------------- -------- -------- ------ -----------------------------------

after which all other routes stopped working for me (see the table above), because they coincide with my that route. How do I make sure my route is processed last?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BoShurik, 2019-02-22
@polyanin

It is necessary to transfer the code from config/routes/annotations.yamlto config/routes.ymland separately register the desired controller at the end of the list:
config/routes.yml

controllers:
    resource: ../src/Controller/
    type: annotation

page_controller:
    resource: ../src/Controller/TextPageController.php
    type: annotation

config/routes/dev/*- everything is standard here
-------------------------- -------- -------- ------ ----------------------------------- 
  Name                       Method   Scheme   Host   Path                               
 -------------------------- -------- -------- ------ ----------------------------------- 
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler_open_file        ANY      ANY      ANY    /_profiler/open                    
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css
// Controllers                    
  index                      ANY      ANY      ANY    /       
// Page Controller                           
  page_show                  ANY      ANY      ANY    /{pageSlug}                        
  page_item                  ANY      ANY      ANY    /{pageSlug}/{parameters}           
 -------------------------- -------- -------- ------ -----------------------------------

Symfony 5.1 introduced the ability to prioritize routes defined via an annotation

V
Vladislav Lyskov, 2019-02-22
@Vlatqa

i do something like this

/**
     * @Route("/{slug}/", requirements={"slug"="[^\s\admin]+"})
     */

D
Denis, 2019-02-22
@prototype_denis

You need your own loader
https://symfony.com/doc/current/routing/custom_rou... A shitty
option, but a working one - ExceptionListener, which will respond to 404s and try to pull your paths from the base by uri

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question