Php 如何将变量传递给注释或yaml?
在Symfony,我们可以:Php 如何将变量传递给注释或yaml?,php,symfony,Php,Symfony,在Symfony,我们可以: /** * @Route( * "/articles/{_locale}/{year}/{slug}.{_format}", * defaults={"_format": "html"}, * requirements={ * "_locale": "en|fr", * "_format": "html|rss", *
/**
* @Route(
* "/articles/{_locale}/{year}/{slug}.{_format}",
* defaults={"_format": "html"},
* requirements={
* "_locale": "en|fr",
* "_format": "html|rss",
* "year": "\d+"
* }
* )
*/
public function show($_locale, $year, $slug)
{
}
以及:
我的问题主要是关于语言
我可以将en | fr从parameters.yml或从PHP(例如,我在数据库中定义了语言)传递到注释和yaml吗
现在,这是代码的重复,另外,如果客户机向数据库添加新语言,那么我必须手动编辑所有路由声明 您可以通过
parameters.yml
或config.yml
传递参数,请参见
反过来说。。。您可以使用变量构建URL。例如,在代码示例中指向您的URL的细枝视图中,您将按照以下方式构建URL
。您可以从控制器获取twig中的参数。因此,在呈现当前视图的控制器中,必须获取所需的参数…;)
article_show:
path: /articles/{_locale}/{year}/{slug}.{_format}
controller: App\Controller\ArticleController::show
defaults:
_format: html
requirements:
_locale: en|fr
_format: html|rss
year: \d+
# app/config/routing.yml
contact:
path: /{_locale}/contact
defaults: { _controller: AppBundle:Main:contact }
requirements:
_locale: '%app.locales%'
# app/config/config.yml
parameters:
app.locales: en|es