我的 symfony2 路由上需要一个前缀变量,以便我可以在主路由文件中执行类似的操作:
//app/config/routing.yml
god:
resource: "@Acme/DemoBundle/Resources/config/routing.yml"
prefix: /god/{religion}
然后在捆绑路由文件中添加类似的内容:
gods_route_to_heaven:
path: /path_to_heaven
defaults: { _controller: AcmeBlogBundle:God:show }
这样我就可以访问这样的路径:
/god/Christianity/path_to_heaven
/god/Islam/path_to_heaven
/god/Hinduism/path_to_heaven
等等。
如果我在控制台上输入
app/console route:debug | grep api
,我会得到正确的路线/god/{religion}/path_to_heaven
,因此路线正在正确生成。
但是当我尝试通过将其作为操作函数中的输入来获取控制器中的参数时,如下所示:
public function showAction($religion)
路径被损坏并转储路径,我看到它被重复:
/god/{religion}/path_to_heaven/{religion}
那么我如何从控制器内部获取 $religion 变量?
你尝试过这个吗?
//app/config/routing.yml
god:
resource: "@Acme/DemoBundle/Resources/config/routing.yml"
prefix: /god
然后在捆绑路由文件中添加类似的内容:
gods_route_to_heaven:
path: /{religion}/path_to_heaven
defaults: { _controller: AcmeBlogBundle:God:show }
如果有效请告诉我。
您需要的可能解决方案是:
文件:src/Acme/CoreBundle/Twig/PathExtension.php
<?php
namespace Acme\CoreBundle\Twig\Extension;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
class PathExtension extends \Twig_Extension
{
private $request;
private $router;
public function __construct(Router $router) {
$this->router = $router;
}
public function onKernelRequest(GetResponseEvent $event) {
if ($event->getRequestType() === HttpKernel::MASTER_REQUEST) {
$this->request = $event->getRequest();
}
}
public function getFunctions()
{
return array(
'path_route' => new \Twig_Function_Method($this, 'getPath')
);
}
public function getPath($name, $parameters = array())
{
$parameters = array_merge($parameters, [
'religion' => $this->request->get('religion'),
]);
return $this->router->generate($name, $parameters, false);
}
public function getName()
{
return 'twig_path_route_extension';
}
}
配置即服务:
文件:src/Acme/CoreBundle/Resources/config/services.yml
services:
twig.path_route_extension:
class: Acme\CoreBundle\Twig\PathExtension
tags:
- { name: twig.extension }
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
arguments: [@router]
设置您的路由选项:
文件:app/config/routing.yml
_acme_religion:
resource: "@AcmeCoreBundle/Resources/config/routing.yml"
prefix: /god/{religion}/
然后你可以在模板和routing.yml中使用它,例如:
_religion_pathheaven:
path: /path_to_heaven
defaults: { _controller: AcmeCoreBundle:System:getpathheaven }
然后,在您的模板中,您可以使用:
<a href="{{ path_route('_religion_pathheaven') }}">
Link Path to heaven.
</a>
参考资料:
自动传递路径参数 http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route
你可以这样解决这个问题-
god:
resource: "@Acme/DemoBundle/Resources/config/routing.yml"
prefix: /god/{_locale}/
此处无需更改--
gods_route_to_heaven:
path: /path_to_heaven
defaults: { _controller: AcmeBlogBundle:God:show }
此处修改--
public function showAction(){
// below line is extra if you need
$post = $this->get('request')->request->all();
$religion = $post['religion'];
}
我没有测试这段代码,但所有过程都得到了很好的证明。如果您发现任何问题,请讨论。
您也可能会看到这个问题,它会对您有很大帮助Symfony2 Route global {_locale} 要求
可能已经晚了,但可以帮助其他人
我在 Symfony 和 FOSUserBundle 上也面临着同样的问题。
在 FOSUserBundle 中,个人资料链接是:
site_url/个人资料/
site_url/个人资料/编辑
site_url/个人资料/更改密码
我有一个管理和仪表板面板,我希望链接位于管理面板中
site_url/admin/profile/
site_url/admin/profile/编辑
site_url/admin/profile/更改密码
在用户仪表板中
site_url/仪表板/个人资料/
site_url/仪表板/个人资料/编辑
site_url/仪表板/个人资料/更改密码
要获得此行为,非常简单,无需 EventListener 和 TwigExtesion
解决办法在这里:
更换
fos_user:
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
由
fos_user_security:
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
fos_user_registration:
resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
fos_user_resetting:
resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
fos_user_change_password:
resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /{prefix}/profile
fos_user_profile:
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /{prefix}/profile
在模板中使用:
<a href="{{ path('fos_user_profile_show', {'prefix': 'dashboard'}) }}">Show Profile</a>
<a href="{{ path('fos_user_profile_edit', {'prefix': 'dashboard'}) }}">Edit Profile</a>
<a href="{{ path('fos_user_change_password', {'prefix': 'dashboard'}) }}">Change Password</a>
或者您想要的任何其他前缀
现在,如果您使用例如更改密码链接,您将收到此错误
模板渲染过程中抛出异常(“Some 缺少用于生成 URL 的强制参数(“前缀”) 路由“fos_user_change_password”。”) FOSUserBundle:ChangePassword:changePassword_content.html.twig 位于行 3.
您可以在模板中添加前缀,如下所示:
更换
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
由
<form action="{{ path('fos_user_change_password', {'prefix': app.request.attributes.get('prefix') }) }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
这会给你这个错误
缺少一些强制参数(“前缀”)来生成 URL 路线“fos_user_profile_show”。
我找到的最终解决方案是覆盖控制器
只需复制应用程序控制器文件夹中的孔控件,更改名称空间并替换
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show');
$response = new RedirectResponse($url);
}
由
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_show',array('prefix'=>$request->get('prefix')));
$response = new RedirectResponse($url);
}
在形式中你不需要执行任何操作,所以它会是
<form {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
你可以这样获取宗教值:
use Symfony\Component\HttpFoundation\Request;
...
public function showAction(Request $request){
$yourReligionIs = $request->get('religion');
}