在 Symfony2 中获取路由需求变量
为了为我的网络界面创建导航,我想从我的包的路由配置中获取一个变量。我在 mybundle/Resources/config/routing.yml 中定义可用页面。
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
现在我查看了 Symfony2 YAML 解析器,我必须为其静态方法解析提供文件路径:http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
我想我可以这样做,因为文件夹层次结构如下所示:
- 我的包
- 控制器
- mycontroller.php
- 资源
- 配置
- 路由.yml
但它不起作用。有什么想法或者可能有其他方法从路由文件中获取requirements.page 数组吗?
问候,本
in order to create a navigation for me webinterface I'd like to get a variable from the routing config of my bundle. I define the available pages in mybundle/Resources/config/routing.yml.
mybundle_homepage:
pattern: /{_locale}/{branch}/{page}
defaults: { _controller: mybundle:mycontroller:index, _locale: de, branch: x.x.x, page: start }
requirements:
_locale: de|en
page: start|functions|events|constants|styleguide
Now I had a look at the Symfony2 YAML Parser and I have to provide a filepath to it's static method parse: http://symfony.com/doc/2.0/reference/YAML.html
mycontroller.php
use Symfony\Component\Yaml\Yaml;
class mycontroller extends Controller
{
public function indexAction($_locale, $branch, $page)
{
$routing = Yaml::parse('../Resources/config/routing.yml');
var_dump($routing);
}
}
I thought I could do it that way because the folder hirarchy looks like that:
- mybundle
- Controller
- mycontroller.php
- Rescources
- config
- routing.yml
But it's not working. Any ideas or maybe another way to get the requirements.page array from the routing file?
Regards, Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够在 DI 容器感知的类中访问路由器服务。所以,你可以这样写:
You should be able to access the router service inside a class that's DI container aware. So, you can write something like: