ZF2 - BootStrap 重定向
ZF2 - BootStrap redirects
我的用户在访问该应用程序之前需要确认他们的电子邮件地址。
我有一个特定的路线,如果他们登录并且他们的电子邮件未得到确认,他们将被发送到该路线:"customer/register-landing"这将发送一封电子邮件,视图将解释他们需要做什么。
为简洁起见,我使用 bootstrap。
这是我目前所拥有的,也是我努力解决的最后一点(重定向部分)
//I run console related queries and this breaks if run
if ( $e->getRequest() instanceof \ZF\ContentNegotiation\Request )
{
//Get a user object
$authService = $sm->get( AuthorizationService::class );
$userObject = $authService->getIdentity();
if (!$userObject instanceof User ) {
return;
}
if ($userObject->getIsEmailConfirmed() == 1) {
return;
}
//So we have a logged in user who needs to confirm their email
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
//Redirect to the route: customer/register-landing
}
}
);
}
我需要做什么才能重定向到实际页面?我环顾四周,发现了这段代码:
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
然而,当我将它添加到 class 时它不起作用:
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
}
}
);
正确的做法是什么?
我用下一个代码解决了这个问题。如果您需要所有代码,请转到 https://github.com/Gimalca/piderapido/blob/master/module/Admin/Module.php
class 模块 {
public function onBootstrap(MvcEvent $e) {
}
public function init(ModuleManager $moduleManager) {
$moduleName = $moduleManager->getEvent()->getModuleName();
if ($moduleName == 'Admin') {
$events = $moduleManager->getEventManager();
$sharedEvents = $events->getSharedManager();
// This define modules need Login
$sharedEvents->attach(array(__NAMESPACE__, 'Admin', 'Account'), 'dispatch', array($this, 'initAuth'), 100);
}
}
public function initAuth(MvcEvent $e) {
//This get router strings
$routerMatch = $e->getRouteMatch();
$module = $routerMatch->getMatchedRouteName();
$controller = $routerMatch->getParam('controller');
$action = $routerMatch->getParam('action');
//This get Authenticate Class
$app = $e->getApplication();
$sm = $app->getServiceManager();
$auth = $sm->get('Admin\Model\LoginAdmin');
// This redirect all. but is login interface not
if ($controller != 'Admin\Controller\Login' && !$auth->isLoggedIn()) {
$controller = $e->getTarget();
return $controller->redirect()->toRoute('admin',array('controller'=>'login','action' => 'index'));
}
if ($auth->isLoggedIn()) {
$viewModel = $e->getViewModel();
$viewModel->userIdentity = $auth->getIdentity();
}
}
我的用户在访问该应用程序之前需要确认他们的电子邮件地址。
我有一个特定的路线,如果他们登录并且他们的电子邮件未得到确认,他们将被发送到该路线:"customer/register-landing"这将发送一封电子邮件,视图将解释他们需要做什么。
为简洁起见,我使用 bootstrap。
这是我目前所拥有的,也是我努力解决的最后一点(重定向部分)
//I run console related queries and this breaks if run
if ( $e->getRequest() instanceof \ZF\ContentNegotiation\Request )
{
//Get a user object
$authService = $sm->get( AuthorizationService::class );
$userObject = $authService->getIdentity();
if (!$userObject instanceof User ) {
return;
}
if ($userObject->getIsEmailConfirmed() == 1) {
return;
}
//So we have a logged in user who needs to confirm their email
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
//Redirect to the route: customer/register-landing
}
}
);
}
我需要做什么才能重定向到实际页面?我环顾四周,发现了这段代码:
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
然而,当我将它添加到 class 时它不起作用:
$redirect = $em->attach(MvcEvent::EVENT_DISPATCH,
function($e){
$route = $e->getRouteMatch();
if ($route->getMatchedRouteName() != 'customer/register-landing')
{
$em->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
$controller = $e->getTarget();
$controller->plugin('redirect')->toRoute('customer/register-landing');
}, 100);
}
}
);
正确的做法是什么?
我用下一个代码解决了这个问题。如果您需要所有代码,请转到 https://github.com/Gimalca/piderapido/blob/master/module/Admin/Module.php
class 模块 {
public function onBootstrap(MvcEvent $e) {
}
public function init(ModuleManager $moduleManager) {
$moduleName = $moduleManager->getEvent()->getModuleName();
if ($moduleName == 'Admin') {
$events = $moduleManager->getEventManager();
$sharedEvents = $events->getSharedManager();
// This define modules need Login
$sharedEvents->attach(array(__NAMESPACE__, 'Admin', 'Account'), 'dispatch', array($this, 'initAuth'), 100);
}
}
public function initAuth(MvcEvent $e) {
//This get router strings
$routerMatch = $e->getRouteMatch();
$module = $routerMatch->getMatchedRouteName();
$controller = $routerMatch->getParam('controller');
$action = $routerMatch->getParam('action');
//This get Authenticate Class
$app = $e->getApplication();
$sm = $app->getServiceManager();
$auth = $sm->get('Admin\Model\LoginAdmin');
// This redirect all. but is login interface not
if ($controller != 'Admin\Controller\Login' && !$auth->isLoggedIn()) {
$controller = $e->getTarget();
return $controller->redirect()->toRoute('admin',array('controller'=>'login','action' => 'index'));
}
if ($auth->isLoggedIn()) {
$viewModel = $e->getViewModel();
$viewModel->userIdentity = $auth->getIdentity();
}
}