Tôi có Trình xử lý đăng xuất nơi tôi muốn đặt thông báo flash hiển thị thông báo xác nhận đăng xuất.Symfony 2 - Cài đặt thông báo Flash ngoài Controller
namespace Acme\MyBundle\Security\Listeners;
use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
class LogoutListener implements LogoutSuccessHandlerInterface
{
private $security;
public function __construct(SecurityContext $security)
{
$this->security = $security;
}
public function onLogoutSuccess(Request $request)
{
$request->get('session')->getFlashBag()->add('notice', 'You have been successfully been logged out.');
$response = new RedirectResponse('login');
return $response;
}
}
Đây là services.yml của tôi (vì nó gắn liền với điều này):
logout_listener:
class: ACME\MyBundle\Security\Listeners\LogoutListener
arguments: [@security.context]
này được tạo ra một lỗi:
Fatal error: Call to a member function getFlashBag() on a non-object
Làm thế nào để tôi đặt một thông điệp flashBag trong này bối cảnh?
Ngoài ra, làm cách nào để có quyền truy cập vào bộ định tuyến để tôi có thể tạo url (qua $ this-> router-> generate ('login')) thay vì chuyển vào url được mã hóa cứng?
Nghị quyết Note
Để có được flash để làm việc, bạn phải nói với cấu hình security.yml của bạn không làm mất hiệu lực phiên giao dịch ngày logout; nếu không, phiên sẽ bị hủy và đèn flash của bạn sẽ không bao giờ xuất hiện.
logout:
path: /logout
success_handler: logout_listener
invalidate_session: false
Tôi đã thêm services.yml của mình. Làm cách nào để thêm dịch vụ? Tôi đoán tôi cần phải sử dụng các đối số, nhưng tôi không chắc chắn về cú pháp để tiêm các đối số này. – doremi
Điều này cho bạn thấy một ví dụ cơ bản về dịch vụ tiêm chích, đọc thêm tại đây: http://symfony.com/doc/current/book/service_container.html –
Phiên của tôi dường như bị phá hủy để đèn flash bị xóa. Tôi nghĩ rằng đăng xuất thực tế đã được thực hiện trước khi trênLogoutSuccess() được gọi là? – doremi