2016-06-30 85 views
1

我想操纵Symfony安全中的登录目标路径,因为我的应用程序使一些AJAX调用。我跟着this documentation article,但没有任何反应(所以我假设security.exception_listener.class没有正确的“挂钩”)。当我GOOGLE时,我发现从2015年github上发现this issue,声称在提供的文档中的解决方案将在Symfony> = 3无效。Symfony3 security.exception_listener.class不工作

现在我只是想知道 - 有没有人知道如果链接文档实际上是过时的(这似乎是这种情况,因为它对我不起作用),以及你如何在Symfony 3中完成同样的事情?

+0

你可以用Symfony的2.8尝试(即目前比3 *,作为一个LTS,至少要等到3.4将出)。只需在您的composer.json中将“symfony/symfony”的约束替换为“2.8。*”并运行作曲者更新 –

+0

我想这是一个选项,尽管它肯定没有诱惑力。但他们不能删除这样一个重要的功能吗? –

+0

您是否找到解决方案?我试着在问题的评论中显示的解决方案,它适用于我。尽管喜欢一个参数。 –

回答

0

的Symfony的2.7文档已被更新,以使用编译通:https://symfony.com/doc/2.7/security/target_path.html

我想,这种变化将很快流过的文档的更新版本。

// src/AppBundle/Security/Firewall/ExceptionListener.php 
namespace AppBundle\Security\Firewall; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\Security\Http\Firewall\ExceptionListener as BaseExceptionListener; 

class ExceptionListener extends BaseExceptionListener 
{ 
    protected function setTargetPath(Request $request) 
    { 
     // Do not save target path for XHR requests 
     // You can add any more logic here you want 
     // Note that non-GET requests are already ignored 
     if ($request->isXmlHttpRequest()) { 
      return; 
     } 

     parent::setTargetPath($request); 
    } 
} 
// src/AppBundle/DependencyInjection/Compiler/ExceptionListenerPass.php 
namespace AppBundle\DependencyInjection\Compiler; 

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; 
use Symfony\Component\DependencyInjection\ContainerBuilder; 
use AppBundle\Security\Firewall\ExceptionListener; 

class ExceptionListenerPass implements CompilerPassInterface 
{ 
    public function process(ContainerBuilder $container) 
    { 
     // Use the name of your firewall for the suffix e.g. 'secured_area' 
     $definition = $container->getDefinition('security.exception_listener.secured_area'); 
     $definition->setClass(ExceptionListener::class); 
    } 
} 
// src/AppBundle/AppBundle.php 
namespace AppBundle; 

use AppBundle\DependencyInjection\Compiler\ExceptionListenerPass; 

class AppBundle extends Bundle 
{ 
    public function build(ContainerBuilder $container) 
    { 
     $container->addCompilerPass(new ExceptionListenerPass()); 
    } 
}