Zend framework 1 article

Home >> Zend framework 1 >>

  Zend framework 1 CRUD operations part - 4

This article will explain you about the CRUD operations of Zend framework 1 in details include topics such as Display blog topics and edit the layout file.

Now, let's flesh out the basic application logic. On a hit to indexAction() methods of BlogController.php controller found in application\controllers\BlogController.php, we'll display all blog topics. This would look like the following:

 /**
  * Display the all blog topics
 */
 public function indexAction()
 {
    $blogTopic = new Application_Model_BlogMapper();
    $this->view->blogTopics = $blogTopic->fetchAll();

    // For meta title.
    $this->view->title = 'View blog topics';
 }

Before displaying the listing of blog topics, we need to add common header and footer in our layout file found in application/layouts/scripts/layout.phtml

In this CRUD operation, I will use bootstrap framework for HTML part, Now, edit the layout file found in application/layouts/scripts/layout.phtml to read as follows:

<?php
// Displaying the docType which we have defined in _initDoctype method of Bootstrap class found in application/Bootstrap.php 
echo $this->doctype()
?>
<html lang="en">
    <head>
        <meta charset="utf-8">
         <meta http-equiv="X-UA-Compatible" c
        <meta name="viewport" c initial-scale=1">
        <title><?php echo $this->title; ?></title>
        <?php
        // This file css/app.global.css is inside public folder.
        $this->headLink()->appendStylesheet($this->baseURL('css/app.global.css'));
        echo $this->headLink();
        ?>
        <!-- Fonts -->
        <link href='//fonts.googleapis.com/css?family=Roboto:400,300' rel='stylesheet' type='text/css'>
        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
                <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
                <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="<?php echo $this->baseURL(); ?>"><b>Zend Framework 1 CRUD Operation</b></a>
                </div>

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="<?php echo $this->baseURL('blog/create'); ?>"><button type="button" class="btn btn-success">Add blog topic</button></a></li>
                    </ul>
                </div>
            </div>
        </nav>

        <div class="container-fluid">
            <div class="row">
                <div class="col-md-10 col-md-offset-1">

                    <?php echo $this->layout()->content ?>

                </div>
            </div>
        </div>

        <!-- Scripts -->
        <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
    </body>
</html>

Now let's move to the view file found in application/views/scripts/blog/index.phtml, where blog topics will be displayed. Edit this file to read as follows:

<?php if (is_array($this->blogTopics) && count($this->blogTopics) >= 1) { ?>  
    <ul class="messages">
        <?php foreach ($this->blogTopics as $key => $value) { ?>
            <li>
                 <div class="message_wrapper"  15px;">
                    <h4 class="heading"><?php echo $value->getTitle(); ?></h4>
                    <blockquote class="message"><?php echo $value->getDescription(); ?></blockquote>
                    <p class="url">
                        <a href="<?php echo $this->baseURL('blog/edit/' . $value->getId()); ?>"><button type="button" class="btn btn-success">Edit</button></a>
                        <a href="<?php echo $this->baseURL('blog/delete/' . $value->getId()); ?>"><button type="button" class="btn btn-danger">Delete</button></a>
                    </p>
                </div>
            </li>
        <?php } ?>
    </ul>
<?php } else { ?>
    <div class="alert alert-info">
        No record found!
    </div>
<?php } ?>

Note: Checkpoint
Now browse to "http://localhost/zf1app/public/blog". You should see the following in your browser:

 

Now, if I want to change my blog home page URL "http://localhost/zf1app/public/" instead of "http://localhost/zf1app/public/blog" then I need to change my routes found in application/configs/routes.php. Open that file and update home page route so it reads as follows:

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

and complete routes.php file look like below.

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

// Add 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 browse to http://localhost/zf1app/public/. You should see the following in your browser:

Write your comment now