How do I reuse code in Zend Framework

Posted by Mario on Stack Overflow See other posts from Stack Overflow or by Mario
Published on 2010-06-15T16:10:25Z Indexed on 2010/06/15 16:22 UTC
Read the original article Hit count: 310

Filed under:
|
|
|

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:

  1. Where is the best place to store code that will be used in multiple controllers?
  2. How do I then call that function from a controller?

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about zend-framework