Welcome, Guest. Register Now!
   
Mark Forums Read Mark Forums Read Mark Forums Read


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-18-2008, 10:09 AM
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default One function for all Actions and Controllers

Hello,

Im sure this is a popular question so I appologise in advance. I can resolve it by creating my own class and then calling that statically in each action and this seems to work OK but I was wondering is there a way to incororate it within the framework.

This is the function I want to run.

PHP Code:
function getPages($section$page)
{
          
$db Zend_Registry::get('db');
       
        try 
        {                                             
            
$sql "SELECT * FROM MainContent WHERE SectionName = '".$section."' AND SubSectionName = '".$page."'";
            
$result $db->fetchAll($sql);
            
$this->view->result $result;
        } 
        catch (
Zend_Exception $e
        {    
            echo 
"Caught exception: " get_class($e) . "<br>";    
            echo 
"Message: " $e->getMessage() . "\n";    
    }

Where do I need to put this and what do I need to extend (if anything) so it is availble for all actions and controllers. Also some advise on how to write and call the custom class and function would be appreciated.

Many thanks in advance.

Madmaxious

Last edited by madmaxious : 04-18-2008 at 10:09 AM. Reason: Canged CODE to PHP
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-18-2008, 01:58 PM
Elemental's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 119
Default

You can create a new base controller class by extending Zend_Controller_Action. Put the new class file in your library under a folder named after your app.

MyApp/Controller/Action.php

PHP Code:
class MyApp_Controller_Action extends Zend_Controller_Action
{
    function 
getPages($section$page){ ... }

Then every controller you want to have access to this function extends your base controller instead of Zend's.

PHP Code:
class MyController extends MyApp_Controller_Action
{
    public function 
indexAction()
    {
        
$this->getPages(...)
    }

__________________
Zend Framework Resources: Zend Webinars | Reference Manual | API Docs | Books | FreeNode: #zftalk
Getting Started Tutorials: Getting started with ZF | Getting started with Zend Auth
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 02:26 PM
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default Great Reply

Thanks for the excellent reply. I shall be trying this first thing Monday morning.

Regards

Madmaxious
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 09:45 PM.