php重定向规则,php – 在access_control规则重定向后通知用户的最佳方法是什么?...

经过不少的研究,我找到了正确的方法。您将需要使用

Entry Point服务并在防火墙配置中进行定义。

此方法不会混淆您的防火墙配置中指定的default page设置以进行登录。

代码

security.yml:

firewalls:

main:

entry_point: entry_point.user_login #or whatever you name your service

pattern: ^/

form_login:

# ...

SRC /阿克米/ UserBundle /配置/ services.yml

entry_point.user_login:

class: Acme\UserBundle\Service\LoginEntryPoint

arguments: [ @router ] #I am going to use this for URL generation since I will be redirecting in my service

SRC /阿克米/ UserBundle /服务/ LoginEntryPoint.php:

namespace Acme\UserBundle\Service;

use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface,

Symfony\Component\Security\Core\Exception\AuthenticationException,

Symfony\Component\HttpFoundation\Request,

Symfony\Component\HttpFoundation\RedirectResponse;

/**

* When the user is not authenticated at all (i.e. when the security context has no token yet),

* the firewall's entry point will be called to start() the authentication process.

*/

class LoginEntryPoint implements AuthenticationEntryPointInterface{

protected $router;

public function __construct($router){

$this->router = $router;

}

/*

* This method receives the current Request object and the exception by which the exception

* listener was triggered.

*

* The method should return a Response object

*/

public function start(Request $request, AuthenticationException $authException = null){

$session = $request->getSession();

//I am choosing to set a FlashBag message with my own custom message.

//Alternatively, you could use AuthenticaionException's generic message

//by calling $authException->getMessage()

$session->getFlashBag()->add('warning', 'You must be logged in to access that page');

return new RedirectResponse($this->router->generate('login'));

}

}

login.html.twig:

{# bootstrap ready for your convenience ;] #}

{% if app.session.flashbag.has('warning') %}

{% for flashMessage in app.session.flashbag.get('warning') %}

×

{{ flashMessage }}

{% endfor %}

{% endif %}

资源:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值