How do I reuse code in Zend Framework
- by Mario
I am working on a web application which requires the user to login before they see or do anything. No part of this app should be accessible without being logged in. (Except of course, the login controller)
Currently I am using sessions to handle the authentication and I have put code in each controller in the init() function to check if their session is valid.
This was a temporary workaround, but it is redundant and inefficient.
I would like my init() function to be similar to the following, but I am not sure how to achieve it:
public function init()
{
// If user not logged in redirect to login controller
$myLibrary = Zend_Library_MyLibrary();
$myLibrary->CheckAuth();
}
So my question really has two parts:
Where is the best place to store code that will be used in multiple controllers?
How do I then call that function from a controller?
Thanks.