Reputation: 1164
Ladies and gentlemen,
I am working hard on a new webapplication which is based on the Zend Framework. Almost the whole webapplication will be secured with a login(username and password).
My idea is to check if the visitor can be authenticated and if not check if the user is requesting a login route. If he is not trying to login he will be redirected to the login page.
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* Bootstrap::_initRouterRewrites()
*
* @return void
*/
protected function _initRouterRewrites()
{
$frontController = Zend_Controller_Front::getInstance();
$this->router = $frontController->getRouter();
$this->router->addRoute(
'default',
new Zend_Controller_Router_Route('/',
array('controller' => 'index',
'action' => 'index'))
)
->addRoute(
'login',
new Zend_Controller_Router_Route('/inloggen.html',
array('controller' => 'auth',
'action' => 'login'))
);
}
/**
* Bootstrap::_initZendSession()
*
* @return void
*/
protected function _initZendSession()
{
// Ensure that both the session and the db resources are bootstrapped
$this->bootstrap(array('db', 'session'));
// Actually start the session
Zend_Session::start();
}
/**
* Bootstrap::_initAuthentication()
*
* @return void
*/
protected function _initAuthentication()
{
// Instantiate auth object
$auth = Zend_Auth::getInstance();
// Check if visitor has an identity
if (!$auth->hasIdentity())
{
}
}
}
When I use the method $this->router->getCurrrentRoute() inthe _initAuthentication method I get the following error: "Current route is not defined".
What can I do to check if the current route is login?
Thanks in advance!
Upvotes: 1
Views: 3590
Reputation: 62
Use plugins like this in bootstrap.php:
protected function _initPlugins() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Plugin_Checkaccess());
}
and in plugins/Checkaccess.php `
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$request = $this->getRequest();
$controllerName = strtolower($request->getControllerName());
$actionName = strtolower($request->getActionName());
if ('user' == $controllerName)
{
$session = new Zend_Session_Namespace('USER');
$notLogged = array('login','register');
if(!in_array($actionName, $notLogged) && !$session->user){
$request->setControllerName("index");
$request->setActionName("index");
$this->_response->setRedirect('/');
//$this->_response->setRedirect('/?redirectTo='.$this->_request->getRequestUri());
}elseif(in_array($actionName, $notLogged) && $session->user){
$request->setControllerName("user");
$request->setActionName("index");
$this->_response->setRedirect('/home');
}
return;
}
} }`
Upvotes: 0
Reputation: 60413
At the time of bootstrapping routing has run yet. What you need here is a FrontController Plugin that hooks into the request lifecycle at the appropriate place . In your case that is probably going to be during routeShutdown
after the routing has determined where to dispatch the request.
From http://devzone.zend.com/1692/zend-framework-mvc-request-lifecycle/:
Upvotes: 4