Hi,
I'm trying to do a simple dispatch->forward to display a log in page if a user is not logged in. I tried doing something similar to what is in the documentation and what is done in the INVO application. I have an ArticleController that displays a blog article that extends from ControllerBase. The initialize function in ControllerBase checks if user is logged in and forwards to the LoginController if user hasn't yet logged in.
The issue is that both the LoginController->indexAction and the ArticleController->displayAction() executes. Is that normal behavior with dispatch->forward()? I know I could do a redirect, but wanted to learn how forward is meant to work.
Something like this:
https://mydomain/article/display/5104db3fe73c844d14000000
class ControllerBase extends Phalcon\Mvc\Controller { protected function initialize() { if (!$this->isLoggedIn()) { $this->forward('login/index'); } }
protected function isLoggedIn()
{
//just a test, will implement session check later
return false;
}
protected function forward($uri)
{
$uriParts = explode('/', $uri);
$this->dispatcher->forward(
array(
'controller' => $uriParts[0],
'action' => $uriParts[1]
)
);
}
}
class ArticleController extends ControllerBase { public function displayAction($article_id) { $this->view->setLayout('threecolumn');
//Fetch and display article
try
{
$articles = Article::findById($id);
$this->view->setVar('title', $articles->title );
$this->view->setVar('description', $articles->description );
$this->view->setVar('date', $articles->date );
$this->view->setVar('author', $articles->author);
$this->view->setVar('text', $articles->text);
}
catch(Exception $ex)
{
//Sorry Not Found Message
$msg = $ex->getMessage();
$this->view->setVar('errors', $msg);
}
}
}
}