Zend framework 1 article

Home >> Zend framework 1 >>

  Zend framework 1 CRUD operations part - 3

This article will explain you about the CRUD operations of Zend framework 1 in details include topics such as Controller, View and Routing.

Controller and View
Controller and view in Zend framework have some naming conventions. All this naming convention is easy to remember, if we use zend commands to do all these stuff because command will do everything for us. But when it comes to do all things manually then we need to follow naming convention, which are given below.

1. Each controller class must be postfix by the Controller. For example, if your controller name is Blog then Controller class must be BlogController.

2. Each and every methods of controller must be postfix by the Action. For example, if your controller function name is index then it must be as indexAction.

3. View folder and file name also depends on Controller name and its methods. 

  • For example, if your controller name is Blog then blog will be your view folder inside application/views/scripts directory.
  • For example, if your controller function name is index then index.phtml will be the view file for that action and it will be inside application/views/scripts/blog directory.

4. Each and every controller action must have it's own view file.

5. Each and every controller class must extend Zend_Controller_Action class.

Let's create controller, To create a new controller, use the zf create controller command:

C:\wamp\www\zf1app>zf create controller Blog
Note: PHPUnit is required in order to generate controller test stubs.
Creating a controller at C:\wamp\www\zf1app/application/controllers/BlogController.php
Creating an index action method in controller Blog
Creating a view script for the index action method at C:\wamp\www\zf1app/application/views/scripts/blog/index.phtml
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'

Now, open the class BlogController found in application/controllers/BlogController.php you will see the below code in this file.

class BlogController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

}

Now we need to add three more methods: save, edit and delete in our BlogController.php to perform save, edit and delete operation. Let's do it by command zf create action action-name controller-name.

C:\wamp\www\zf1app>zf create action save Blog
Note: PHPUnit is required in order to generate controller test stubs.
Creating an action named save inside controller at C:\wamp\www\zf1app/application/controllers/BlogController.php
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'
Creating a view script for the save action method at C:\wamp\www\zf1app/application/views/scripts/blog/save.phtml
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'

C:\wamp\www\zf1app>zf create action edit Blog
Note: PHPUnit is required in order to generate controller test stubs.
Creating an action named edit inside controller at C:\wamp\www\zf1app/application/controllers/BlogController.php
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'
Creating a view script for the edit action method at C:\wamp\www\zf1app/application/views/scripts/blog/edit.phtml
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'

C:\wamp\www\zf1app>zf create action delete Blog
Note: PHPUnit is required in order to generate controller test stubs.
Creating an action named delete inside controller at C:\wamp\www\zf1app/application/controllers/BlogController.php
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'
Creating a view script for the delete action method at C:\wamp\www\zf1app/application/views/scripts/blog/delete.phtml
Updating project profile 'C:\wamp\www\zf1app/.zfproject.xml'

Now, open the class BlogController found in application/controllers/BlogController.php you will see the below code in this file.

class BlogController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function saveAction()
    {
        // action body
    }

    public function editAction()
    {
        // action body
    }

    public function deleteAction()
    {
        // action body
    }

}

Now, check the view folder /blog found in application/view/scripts to check the views of Blog controller. The all view will follow the controller and view naming convention of Zend framework as explained above.

application/view/scripts/blog/index.phtml
application/view/scripts/blog/save.phtml
application/view/scripts/blog/edit.phtml
application/view/scripts/blog/delete.phtml

Routing
Routes. A route is a URL pattern that is mapped to a handler. The handler can be a physical file, in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application.

We will use the below routing fo CRUD operation.

1. http://localhost/zf1app/public/blog :  display the all blog topics.
2. http://localhost/zf1app/public/blog/create : creating new blog topic.
3. http://localhost/zf1app/public/blog/edit : edit blog topic.
4. http://localhost/zf1app/public/blog/delete : delete blog topic.

Create routes.php inside application/configs folder and edit this file to read as follows

// Blog home page route
$route = new Zend_Controller_Router_Route(
        'blog', array(
            'controller' => 'blog',
            'action' => 'index'
        )
);
$router->addRoute('blog', $route);

// Save blog route
$route = new Zend_Controller_Router_Route(
        'blog/create', array(
            'controller' => 'blog',
            'action' => 'save'
        )
);
$router->addRoute('blog/create', $route);

// Edit blog route
$route = new Zend_Controller_Router_Route(
        'blog/edit/:id', array(
            'controller' => 'blog',
            'action' => 'edit'
        ), array('id' => '\d+')
);
$router->addRoute('blog/edit/:id', $route);

// Delete blog route
$route = new Zend_Controller_Router_Route(
        'blog/delete/:id', array(
            'controller' => 'blog',
            'action' => 'delete'
        ), array('id' => '\d+')
);
$router->addRoute('blog/delete/:id', $route);

Now we will inform Bootstrap class about routes, so we'll create an _initRoutes() method within our bootstrap class application/Bootstrap.php 

    /**
     * Application routing
     */
    protected function _initRoutes()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        include APPLICATION_PATH . "/configs/routes.php";
    }

Finally Bootstrap class will look like below

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    /**
     * Init the doctype.
     */
    protected function _initDoctype()
    {
        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->doctype('XHTML1_STRICT');
    }

    /**
     * Application routing
     */
    protected function _initRoutes()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        include APPLICATION_PATH . "/configs/routes.php";
    }

}

 

Write your comment now