Wednesday, May 19, 2010

Processing a controller action


In this example, we create a controller and a view script. The controller passes the URL parameters to the view script.

Components used in this example
Using the action controller

class MyActionController
{
    const 
BASE_URL 'http://domain/';

    public static 
$uri = array(
        
'my/script/param1/one/param2/two',
        
'my/script/param1/123',
        
'my/script/param1/456',
    );
Processing the controller action
  • We get the URL.
  • We create the front controller.
  • We create the request.
  • We dispatch the action and we get the rendered data.
  • If we catch an exception, we return the error message.

    public function process()
    {
        
// We get the URL.
        
list($uri) = $this->_getParameters();

        try {
            
// We create the front controller.
            
$front Zend_Controller_Front::getInstance();
            
$front->setControllerDirectory('.')
                ->
returnResponse(true);

            
// We create the request.
            
$request = new Zend_Controller_Request_Http(self::BASE_URL $uri);

            
// We dispatch the action and we get the rendered data.
            
$result $front->dispatch($request, new Zend_Controller_Response_Http)->getBody();

            } catch (
Exception $e) {
            
// If we catch an exception, we return the error message.
            
$result $e->getMessage();
        }

        return array(
$uri$result);
    }
Extraction of the parameters from the GET request

    private function _getParameters()
    {
        
$uri = isset($_GET['uri'])? $_GET['uri'] : self::$uri[0];

        return array(
$uri);
    }

}

No comments:

Post a Comment