So, the question is how can I configure Module.php in my module to check if the user is or is not in session? If he is not I want to redirect him to the log in page.
I don't want the user to has permission to go on other action(controller) if he is not in session(not logged in).
This should be done with event in ZF2 for more details: click here also this code may help you. http://pastebin.com/FFGVCpki
public function init() {
// Attach Event to EventManager
$events = StaticEventManager::getInstance ();
// Add event of authentication before dispatch
$events->attach ( 'Zend\Mvc\Controller\AbstractActionController', 'dispatch', array (
$this,
'authPreDispatch'
), 110 );
}
public function authPreDispatch($event){
$target = $event->getTarget ();
$serviceLocator = $target->getServiceLocator();
// Do what ever you want to check the user's identity
$url = $event->getRouter ()->assemble ( array (
"controller" => "<controller>"
), array (
'name' => '<route name>'
) );
$response = $event->getResponse ();
$response->setHeaders ( $response->getHeaders ()->addHeaderLine ( 'Location', $url ) ));
$response->setStatusCode ( 302 );
$response->sendHeaders ();
exit ();
}
don't use redirect, do setParam() method using 'route' event, see this https://github.com/samsonasik/SanAuthWithDbSaveHandler/commit/e2ae4dfcebb7a952d7b1adaadcf6496c994423b9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With