更改路线加载顺序的最佳方法
Best way to change routes loading order
我的控制器中的路由已加载到 /config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
我在 /config/routes.yaml
中有路由
about:
path: /about
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: front/about.html.twig
...
"controllers" 中的动态路由覆盖了我的 "static" 路由。
在控制器中的路由之前加载 "static" 路由的最佳方法是什么。
我通过评论 /config/routes/annotations.yaml 中的内容并将其粘贴到 /config/routes.yaml 的末尾来使其工作,但我认为这不是最好的方法.. .
从技术上讲,这可能还不够。在此之后加载的任何路由都将被忽略,如果您使用注释,则必须将此操作放在按字母顺序排序的最后一个控制器的最后一个操作中。
在yml中配置这条路由,放在routes.yml末尾。
这条路线将是最后执行的(性能命中),它会捕获所有请求,因此请确保正确抛出 404-s。
(我假设客户希望能够完全配置路由是否正确?例如 CMS 页面?有过几次这种情况)
它不想更改我的 url 也不想使用评论中建议的字母技巧。
我通过更改内核中导入路由的顺序修复了它。
而不是:
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
}
我
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
# routes loaded in routes.yaml
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
# routes loaded in routes/annotations.yaml
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
}
你可以通过在routes.xml中的静态路由之后放置带有注解的控制器来避免改变内核逻辑:
browserconfig:
path: /browserconfig.xml
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: browserconfig.xml.twig
app_document:
resource: App\Controller\DocumentController
type: annotation
我的控制器中的路由已加载到 /config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
我在 /config/routes.yaml
中有路由about:
path: /about
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: front/about.html.twig
...
"controllers" 中的动态路由覆盖了我的 "static" 路由。
在控制器中的路由之前加载 "static" 路由的最佳方法是什么。
我通过评论 /config/routes/annotations.yaml 中的内容并将其粘贴到 /config/routes.yaml 的末尾来使其工作,但我认为这不是最好的方法.. .
从技术上讲,这可能还不够。在此之后加载的任何路由都将被忽略,如果您使用注释,则必须将此操作放在按字母顺序排序的最后一个控制器的最后一个操作中。
在yml中配置这条路由,放在routes.yml末尾。
这条路线将是最后执行的(性能命中),它会捕获所有请求,因此请确保正确抛出 404-s。
(我假设客户希望能够完全配置路由是否正确?例如 CMS 页面?有过几次这种情况)
它不想更改我的 url 也不想使用评论中建议的字母技巧。
我通过更改内核中导入路由的顺序修复了它。
而不是:
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
}
我
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
# routes loaded in routes.yaml
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
# routes loaded in routes/annotations.yaml
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
}
你可以通过在routes.xml中的静态路由之后放置带有注解的控制器来避免改变内核逻辑:
browserconfig:
path: /browserconfig.xml
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController::templateAction
defaults:
template: browserconfig.xml.twig
app_document:
resource: App\Controller\DocumentController
type: annotation