polyanin
@polyanin
Golang, PHP & Symfony developer

Как задать приоритет маршрутов (маршрутизации)?

Приветствую!

Делаю сайт, у которого адреса страниц и страницы хранятся в базе данных.
По примеру https://symfony.com/doc/current/routing/slash_in_p... сделал контроллер
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
 -------------------------- -------- -------- ------ -----------------------------------


после чего у меня перестали работать все остальные маршруты (см. таблицу выше), потому, что они совпадают с моим тем маршрутом. Как устроить так, чтобы мой маршрут обрабатывался последним?
  • Вопрос задан
  • 833 просмотра
Решения вопроса 1
BoShurik
@BoShurik Куратор тега Symfony
Symfony developer
Надо перенести код из config/routes/annotations.yaml в config/routes.yml и отдельно прописать нужный контроллер в конце списка:
config/routes.yml
controllers:
    resource: ../src/Controller/
    type: annotation

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


config/routes/dev/* - тут все стандартно

-------------------------- -------- -------- ------ ----------------------------------- 
  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 появилась возможность выставлять приоритет у роутов, заданных через аннотацию
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
я делаю примерно так
/**
     * @Route("/{slug}/", requirements={"slug"="[^\s\admin]+"})
     */
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы