Registering your custom plugins in Zend Framework 1.8

When Zend Framework 1.8 was released, anyone could now setup a project within seconds using the zf Command Line Tool — see Quickstart Guide.

A few days ago, I managed to setup a project using the new command line tools. My project now had models, views and all the controllers that I needed. But then came the time when I had to setup my own custom plugins. Now, I had done this couple of times before. Prior to the 1.8 release, there was no command line tool and everyone had custom bootstrap files and their own configuration. The common thing to do was to do something like this: (maybe in the your bootrap file)

$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new FooPlugin());

Now, after then ZF1.8 release, ZF generated the bootstrap files for you, I thought it was probably a good idea not to edit the default files it created.

To register your own custom plugin in the new Zend Framework 1.8 release, go to you application.ini file found in the configs directory and add this:

autoloaderNamespaces[] = "My_"
resources.frontController.plugins.CheckHasAcess = "My_Controller_Plugin_CheckHasAccess"

This is assuming that you have your plugin in this directory:

/path/to/library/My/Controller/Plugin/CheckHasAccess.php

And the CheckHasAccess.php file contains something like this.

<?php
/**
 * Enter description here...
 *
 */
class My_Controller_Plugin_CheckHasAccess extends Zend_Controller_Plugin_Abstract {
 
    /**
     * Enter description here...
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        //code for your plugin here... :P
    }
}

Please note that Zend Framework has a naming convention on this. For the classname: My_Controller_Plugin_CheckHasAccess, every underscode (_) is converted into a slash (/) to form a directory structure. So, the classname ‘My_Controller_Plugin_CheckHasAccess’ would be read as /My/Controller/Plugin/CheckHasAccess.php by Zend Framework — with the ‘My’ folder inside the ‘library’ folder

So the key things to remember are:

  1. Register your custom plugin in your application.ini
  2. Make sure that you load your namespace in the application.ini. In this example, it should be ‘My’
  3. Make sure that you have your classname-to-directory structure is correct

FURTHER READING:
Zend Devzone has a very good article discussing about the custom Front Controller Plugin. It can be found here.

Zend Framework Best Practices: Where to put the Zend Framework Source Code?

Very good question. Very good post.

As quoted from Devzone:

I was having a conversation with a client recently on the topic of best practices. He asked me:

Where should I put the Zend Framework source code?

This is an excellent question that can not only be applied to Zend Framework, but to similar frameworks also. The question has in fact two answers in my opinion, which answer is right for you depends on how you answer this question.

Is Zend Framework part of your application or part of your PHP environment?
If your application or organization matches any of the following:

  • You have no control over the PHP environment your application will be installed into. For example you intend to distribute your application (e.g. Magento, phpMyAdmin).
  • Your development team has low communication with your IT department.
  • Once you’ve deployed your application you’re not going to touch it for long periods of time (common for consulting companies).

In these situations Zend Framework should be considered as a part of your application.

If you’re working on an application and organization where:

  • You do have control over the environment.
  • You are actively working on the application.
  • The IT department and the development team are in good sync.
  • You will want to make Zend Framework a part of your PHP environment.

Click here to view the entire post.

Zend Framework: Making the Built-in Breadcrumb Helper Work

In the latest release of Zend Framework (1.8.1 as of this writing), there is a new built-in view helper to render your breadcrumb needs. There is a section in the documentation that tells us how to use it. But I got a little confused when I tried to make it work.

So, here is a short example on how I made my breadcrumbs work using the new breadcrumb view helper from Zend Framework.

First off, I want to render these links:

Before you are able to render your breadcrumb, you must create a container for it first. For the breadcrumbs above, you can have something that will look like this:

// I have placed this code somewhere in:
// application/layouts/scripts/layout.phtml
$container = new Zend_Navigation();
$this->navigation($container);
 
$container->addPage(
    array(
        'label'      => 'Dashboard',
        'module'     => 'default',
        'controller' => 'dashboard',
        'action'     => 'index',
        'pages'      =>
        array(
            array(
                'label'      => 'Create Order',
                'module'     => 'default',
                'controller' => 'createorder',
                'action'     => 'index'
            ),
            array(
                'label'      => 'Query',
                'module'     => 'default',
                'controller' => 'query',
                'action'     => 'index',
                'pages' => array(
                    array(
                        'label'      => 'View Order',
                        'module'     => 'default',
                        'controller' => 'order',
                        'action'     => 'vieworder'
                    )
                )
            ),
            array(
                'label'      => 'Administration',
                'module'     => 'default',
                'controller' => 'admin',
                'action'     => 'index',
                'pages'      =>
                array(
                    array(
                        'label'      => 'News and Announcements',
                        'module'     => 'default',
                        'controller' => 'admin',
                        'action' => 'addnews',
                        'pages' => array(
                            array(
                                'label'      => 'Edit News and Announcements',
                                'module'     => 'default',
                                'controller' => 'admin',
                                'action' => 'editnews'
                            )
                        )
                    )
                )
            )
        )
    )
);
 
//Finally, echo the breadcrumb!
echo '<div class="breadcrumbs">';
echo 'You are in: '.$this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render();
echo '</div>';

A breadcrumb should appear if you are in the pages specified in the $container array.

Update:
I have a new post using Zend_Navigation and breadcrumbs using an XML file. Using an XML file makes more sense than creating the huge array above. The post can be found here.

How to Build a Facebook App using Zend Framework

Here is a quick tutorial on how to create a Facebook App using Zend Framework. I am just going to link directly to the post. Click Here.

Zend Framework: Zend_Soap / Create a Web Service

Here is a quick tutorial on how to create your own web service using Zend Framework.

Web services are software systems designed to support interoperable machine-to-machine interaction over a network. Nowadays if you want to connect external systems, you probably want or have to use web services. What I will discuss here is how to get your own SOAP web service up in minutes.

SOAP(Simple Object Access Protocol ) is probably the most used web service protocol today. It relies on XML as its message format, and it uses HTTP for message transmission. The SOAP server uses WSDL(Web Services Description Language ) to describe its services to external clients. WSDL is simply an XML-based language that provides a model for describing Web services.

Back in the old days you had to know a lot about SOAP and WSDL create a web service. Have a look at http://www.php.net/soap to see what I mean. Definitely not very good looking. Luckily Zend Framework has a nice component, Zend_Soap, that handles all the SOAP hard work you would be supposed to do.

The thing I like about this tutorial is that it uses the Zend Framework MVC. See the code below…

require_once realpath(APPLICATION_PATH .
'/../library/').'/Soaptest.php';
 
class SoapController extends Zend_Controller_Action
{
    //change this to your WSDL URI!
   private $_WSDL_URI="http://192.168.188.128:8081/soap?wsdl";
 
    public function indexAction()
    {    
        $this->_helper->viewRenderer->setNoRender();
 
        if(isset($_GET['wsdl'])) {
            //return the WSDL
            $this->hadleWSDL();
        } else {
            //handle SOAP request
            $this->handleSOAP();
        }
    }
 
    private function hadleWSDL() {
        $autodiscover = new Zend_Soap_AutoDiscover();
        $autodiscover->setClass('Soaptest');
        $autodiscover->handle();
    }
 
    private function handleSOAP() {
        $soap = new Zend_Soap_Server($this->_WSDL_URI); 
        $soap->setClass('Soaptest');
        $soap->handle();
    }
 
    public function clientAction() {
        $client = new Zend_Soap_Client($this->_WSDL_URI);
 
        $this->view->add_result = $client->math_add(11, 55);
        $this->view->not_result = $client->logical_not(true);
        $this->view->sort_result = $client->simple_sort(
       array("d" => "lemon", "a" => "orange",
             "b" => "banana", "c" => "apple"));
 
    }
 
}
 
/* 
 *
 * For The TEST CLASS 
 *
 */
class Soaptest {
    /**
     * Add method
     *
     * @param Int $param1
     * @param Int $param2
     * @return Int
     */
    public function math_add($param1, $param2) {
        return $param1+$param2; 
    }
 
    /**
     * Logical not method
     *
     * @param boolean $param1
     * @return boolean
     */
    public function logical_not($param1) {
        return !$param1;
    }
 
    /**
     * Simple array sort
     *
     * @param Array $array
     * @return Array
     */
    public function simple_sort($array) {
        asort($array);
        return $array;
    }
 
}

A full explanation of code can be found in Bogdan Abei’s blog.

Thanks to the author for sharing this ;-)


© Copyright 2007 eKini: Web Developer Blog . Thanks for visiting!