2016-06-30 123 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()); 
    } 
}