我有一个内核事件监听器设置(kernel.controller)来重定向用户,如果他没有登录.事件监听器成功被调用,但是我很难搞清楚如何重定向.这是我得到的:
$cont = $event->getController('testpost');
$event->setResponse($cont);
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
Fatal error: Call to undefined method Symfony\Component\HttpKernel\Event\FilterControllerEvent::setResponse()
Run Code Online (Sandbox Code Playgroud) 我正在使用FOSUserBundle软件包使用Symfony 2构建Web应用程序.
用户创建帐户,登录并开始使用该应用程序.
我现在想要实现的是让用户从他们登录的任何页面重定向到他们的帐户.
这包括:
基本上代码是这样的:
$container = $this->container;
$accountRouteName = "DanyukiWebappBundle_account";
if( $container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
// authenticated (NON anonymous)
$routeName = $container->get('request')->get('_route');
if ($routeName != $accountRouteName) {
return $this->redirect($this->generateUrl($accountRouteName));
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道该代码应该去哪里.
它应该针对任何请求执行.在Symfony 1中我会使用过滤器.
如何在页面加载甚至监听器中获取路由?
我在services.yml中有以下代码:
page_load_listener:
class: Acme\MainBundle\EventListener\PageLoadListener
arguments: [@security.context, @session]
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController, priority: 64 }
Run Code Online (Sandbox Code Playgroud)
在PageLoadListener类中,我有相应的方法:
public function onKernelController(FilterControllerEvent $event)
{
// Some code I need to execute that needs the route arguments
}
Run Code Online (Sandbox Code Playgroud)
问题似乎是路由和参数不可用.我错过了什么?
我需要通过哪个事件来获取路线及其参数?
谢谢,
JB
而不是错误页面/ 404我想只显示/ sitemap页面.当然我不想重定向,我仍然希望设置404 HTTP响应头.
这可能吗?我只能看到如何在Twig中设置模板.
我绝对不想要重定向.
我仍然有一些问题需要在symfony中处理我的资产.在最佳实践说,我应该保存我的资产web/.但我不喜欢在那里存储我的原始sass文件,因为它是一个公共文件夹,我认为只应该存储编译或静态文件.
这就是我存储它们(js和sass)的原因app/Resources.我的assetic.read_from是app/Resources.但是有一些捆绑,由符号链接assets:install到web/bundles/.
现在,当我想在我的twig文件中包含这个bundle-assets时,我必须../../web/bundles/..在样式表块中去那里.这看起来不是很干净,所以我做了一个符号链接 app/Resources/bundles->web/bundles/,这是有效的.
但我仍然认为它太过小巧,我想知道是否有一种更清洁的方式可以更好地在一个地方收集我的资产.