Zend controller

From EKiniWiki

Jump to: navigation, search

Notes for Zend_controller. This section needs more information.

For an overview of the directory structure, see: Zend Framework Directory Structure

[edit] Creating your own controller

For creating your own controller, see this: Zend Controller Quickstart.

Here is an example based on Akrabat's Tutorial:

Filename: /application/controllers/IndexController.php

class IndexController extends Zend_Controller_Action
{
    function init() {
        $this->initView();
        $this->view->baseUrl = $this->_request->getBaseUrl();
    }
    
    function indexAction() 
    {
        $this->view->title = "My Dashboard";
        
        $this->render();
    } 

    //more code will go here...
}

Then, Filename: /application/views/scripts/index/index.phtml

...
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title><?=$this->title?></title>
</head>
...
rest of html code here
...

[edit] Flash Messenger Plugin

This plugin is used to display error/notice messages between requests. The message will be displayed on the next action and then deleted after it is being displayed.

<?php
class SomeController extends Zend_Controller_Action
{
    /**
     * FlashMessenger
     *
     * @var Zend_Controller_Action_Helper_FlashMessenger
     */
    protected $_flashMessenger = null;
    
    
    public function init()
    {
        $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $this->initView();
    }
    
    public function myAction()
    {
        /**
         * default method of getting Zend_Controller_Action_Helper_FlashMessenger
         * instance on-demand
         */
        $this->_flashMessenger->addMessage('Record Saved!');
    }

    public function myNextRequestAction()
    {
        $this->view->messages = $this->_flashMessenger->getMessages();
        $this->render();
    }
}

Then, in the myNextRequest.phtml OR any view file, I would have something like this:

...html code here...
<?php
if (!empty($this->messages[0])) {
    echo '<div class="flashmessenger">';
    echo $this->messages[0];
    echo '</div>';
}
?>
...more html code here...

[edit] Create the Action Helper

Inside MyAccessControlHelper.php do something like this:

<?php
require_once('Zend/Controller/Action/Helper/Abstract.php');
require_once('Zend/Loader.php');
require_once('default/Models/Permissions.php');
require_once('default/Models/Resources.php');
require_once('default/Models/Roles.php');
require_once('default/Models/UserRole.php');
require_once('default/Models/Users.php');

//READ ME
//The class name is actually the same as the directory path we have created above.
//Only that the /s (slashes) are replaced with _'s (underscores)
//So, this file is located in:
//   library/Ekini/Controller/Action/Helper/MyAccessControlHelper.php
  

class Ekini_Controller_Action_Helper_MyAccessControlHelper extends Zend_Controller_Action_Helper_Abstract {

    /**
     * check if logged in user has access. redirects to $redirect if no access
     * returns null if no result otherwise returns the resultset
     *
     */
    public function hasAccess($redirect = '/')
    {
        Zend_Loader::loadClass('Zend_Auth');
        
        try {
            
            $dbAdapter = Zend_Registry::get('dbAdapter');
            
            $moduleName     = $this->getRequest()->getModuleName();
            $controllerName = $this->getRequest()->getControllerName();
            
            //get the user identity, this is null if the user is not logged in
            $user = Zend_Auth::getInstance()->getIdentity();
            
            if ($user) {
                if ($user->id=='1') {
                    return true;
                }    
                
                //get all user roles for logged in user
                $userRoles = new UserRole();
                $roles = $userRoles->fetchAll('user_id="'.$user->id.'"');
                
                foreach ($roles as $role) {

                    $sql = "
                        SELECT p.*, r.*, u.*, rs.*
                        FROM users_to_roles ur
                        LEFT JOIN permissions p ON p.role_id = ur.role_id
                        LEFT JOIN users u ON ur.user_id = u.id
                        LEFT JOIN roles r ON p.role_id =  r.id
                        LEFT JOIN resources rs ON p.resource_id = rs.id
                        WHERE ur.role_id=".$role->role_id."
                        ";
            
                    $acl = $dbAdapter->fetchAll($sql);
                    
                    foreach ($acl AS $row) {
                        if ($row['resource_name'] == $controllerName AND $row['module'] == $moduleName AND $row['access'] == 'allow') {
                            return true;
                        } else if ($row['resource_name'] == $controllerName AND $row['module'] == $moduleName AND $row['access'] == 'deny') {
                            throw new Exception('You are not allowed to access this page. (Group Denied)')   ;
                        } else {
                            throw new Exception('You are not allowed to access this page. (Denied)')   ;
                        }
                    }
                
                }
            } else {
                throw new Exception('You are not logged in.')   ;
            }
            
            
        } catch (Exception $e) {
            //$this->_flashMessenger->addMessage($e->getMessage());
            
            //im just doing this since i don't know how to redirect yet :(
            echo $e->getMessage();
            die();
            
            //$this->_redirect($redirect);
            //THIS WONT WORK
        }
    }
}

NOTE: Please read the comment just above the class name

   class Ekini_Controller_Action_Helper_MyAccessControlHelper extends Zend_Controller_Action_Helper_Abstract {

Accessing it would be something like:

   $this->getHelper('MyFirstActionHelper')->hasAccess();
Personal tools
Bookmarks