2012-01-29 11 views
21

लॉगिन सफलता के लिए use_referer: true पैरामीटर है। लॉगिन विफलता के लिए केवल failure_path है, जो मैं नहीं ढूंढ रहा हूं।लॉगिन विफलता के बाद रेफरर पर वापस कैसे जाएं?

दूसरी बात: यह कैसे करें और त्रुटि संदेश पास करें?

तीसरी बात: लॉगआउट के बाद रेफरर पर वापस कैसे जाएं?

+0

विफलता पथ केवल लॉगिन प्रॉम्प्ट क्यों नहीं होगा? – JamesHalsall

+1

क्योंकि मैं प्रत्येक उपपृष्ठ से लॉगिन कर सकता हूं। मुझे निर्दिष्ट यूआरएल पर एक लॉगिन फॉर्म नहीं मिला है, इसलिए जब मैं लॉगिन करता हूं तो उपयोगकर्ता को होमपेज पर रीडायरेक्ट नहीं करना चाहता, उदाहरण के लिए किसी अन्य उपयोगकर्ता प्रोफाइल पेज। –

उत्तर

49

मैंने इसे हल किया।

समाधान नहीं है: How to disable redirection after login_check in Symfony 2

और यहाँ कोड है जो मेरी समस्या का हल है:

form_login: 
    check_path: /access/login_check 
    login_path:/
    use_referer: true 
    failure_handler: authentication_handler 
logout: 
    path: /access/logout 
    target:/
    success_handler: authentication_handler 

और विन्यास के लिए:

<?php 

namespace Acme\MainBundle\Handler; 

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface; 
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; 
use Symfony\Component\Security\Core\Exception\AuthenticationException; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 

class AuthenticationHandler implements AuthenticationFailureHandlerInterface, LogoutSuccessHandlerInterface 
{ 
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception) 
    {  
     $referer = $request->headers->get('referer');  
     $request->getSession()->setFlash('error', $exception->getMessage()); 

     return new RedirectResponse($referer); 
    } 

    public function onLogoutSuccess(Request $request) 
    { 
     $referer = $request->headers->get('referer'); 
     return new RedirectResponse($referer); 
    } 
} 

घटनाओं उदाहरण के लिए security.yml में जोड़ने को संभालने के लिए .yml:

services: 
    authentication_handler: 
     class: Acme\MainBundle\Handler\AuthenticationHandler 
+4

$ अनुरोध-> शीर्षलेख-> प्राप्त करें ('रेफरर'); कभी-कभी शून्य लौटाता है (मैंने फ़ायरफ़ॉक्स में पुन: उत्पन्न किया है)। यह समाधान विश्वसनीय नहीं है, फॉलबैक या कोई अन्य समाधान आवश्यक है। – Julien

+0

@ wojciech-kulik मेरे पास निम्न त्रुटि है: '' $ अनुरोध-> getSession() -> setFlash ('त्रुटि', $ अपवाद-> getMessage()); '' '' FatalErrorException: त्रुटि: अपरिभाषित विधि पर कॉल करें सिम्फनी \ घटक \ HttpFoundation \ सत्र \ सत्र :: setFlash() /var/www/symfony/src/Application/Sonata/UserBundle/Form/Handler/AuthenticationHandler.php लाइन 16''' – jcarlosweb

+0

@webyseo शायद आपके पास सिम्फनी का एक अलग संस्करण है और फ्लैश संदेश एक अलग तरीके से सेट हैं। शायद यह मदद करेगा: http://stackoverflow.com/questions/13348534/symfony-2-setting-a-flash-message-outside-of-controller –

संबंधित मुद्दे