gpt4 book ai didi

model-view-controller - ZF2 : Listen for an MVC Exception, 然后重定向

转载 作者:行者123 更新时间:2023-12-03 08:26:39 25 4
gpt4 key购买 nike

在我的 ZF2 应用程序中,我有时会抛出 LoginRequired异常(exception)。我想捕捉这个异常并用它来将用户重定向到登录页面。我该怎么做?

该错误通常是由我的服务层引发的。它通常在调度期间抛出,但也可能在渲染期间抛出,因为有一个 View 助手调用一些服务层方法(例如,获取当前用户)。

我原以为使用模块的引导方法将监听器附加到 dispatch.error 是一件简单的事情。和 render.error事件。然后我打算测试 LoginRequired异常,但我似乎无法掌握实际的异常。 ( $mvcEvent->getResult()->exception 返回 null )。

最佳答案

我有一个示例监听器聚合应该有所帮助。

不同之处在于我的监听器使用配置和匹配的路由名称来确定是否应该发生重定向。您可以改为在 dispatch.error 期间,使用 $mvcEvent->getParam('exception') 从 MVC 事件中获取异常并检查。

use ArpAuth\Service\AuthenticationService;
use ArpAuth\Service\AuthenticationServiceAwareTrait;
use Zend\Console\Console;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\EventManager\AbstractListenerAggregate;


class NoAuthRedirectStrategy extends AbstractListenerAggregate
{

protected $whitelistRoutes = [];
protected $redirectRoute = 'user/auth/login';

use AuthenticationServiceAwareTrait;

public function __construct(AuthenticationService $authenticationService, array $options = [])
{
$this->authenticationService = $authenticationService;

if (! empty($options)) {
$this->setOptions($options);
}
}


public function attach(EventManagerInterface $eventManager)
{
$eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'isAuthenticated'], 1000);
}

public function isAuthenticated(MvcEvent $event)
{
$routeMatch = $event->getRouteMatch();

if (Console::isConsole() || ! $routeMatch instanceof RouteMatch) {
return;
}

$serviceManager = $event->getApplication()->getServiceManager();
$currentRoute = $routeMatch->getMatchedRouteName();
$isRemoteApiCall = $serviceManager->get('IsRemoteApiCall');

if ($this->hasWhitelistRoute($currentRoute) || $isRemoteApiCall || $this->authenticationService->hasIdentity()) {
return;
}

$router = $event->getRouter();
/** @var Response $response */
$response = $event->getResponse();

$url = $router->assemble([], ['name' => $this->redirectRoute]);

$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);

return $response;
}

public function setRedirectRoute($redirectRoute)
{
$this->redirectRoute = $redirectRoute;

return $this;
}

public function hasWhitelistRoute($route)
{
return (! empty($route) && in_array($route, $this->whitelistRoutes));
}

public function getWhitelistRoutes()
{
return $this->whitelistRoutes;
}

public function setWhitelistRoutes(array $routes)
{
$this->whitelistRoutes = [];

return $this->addWhitelistRoutes($routes);
}

public function addWhitelistRoutes(array $routes)
{
foreach($routes as $route) {
$this->addWhitelistRoute($route);
}
return $this;
}

public function addWhitelistRoute($route)
{
if (! $this->hasWhiteListRoute($route)) {
$this->whitelistRoutes[] = $route;
}

return $this;
}

public function setOptions(array $options)
{
if (isset($options['whitelist']) && is_array($options['whitelist'])) {
$this->setWhitelistRoutes($options['whitelist']);
}

if (isset($options['redirect_route'])) {
$this->setRedirectRoute($options['redirect_route']);
}

return $this;
}
}

关于model-view-controller - ZF2 : Listen for an MVC Exception, 然后重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com