PHP Frameworks: Zend Framework

More so than the other two frameworks reviewed in this chapter, Zend Framework takes a “collection of components” approach. While this makes it easy to drop a com­ponent or two into an existing project without conforming to a particular file struc­ture or request routing convention, it also means starting from scratch is a little more complicated.

To install a Zend Framework “skeleton” app into the menu directory that contains the basics necessary to get up and running, run the following Composer command all on one line:

composer create-project –no-interaction –stabitity=”dev”

zendframework/skeieton-appiication menu

Then, to make the built-in PHP web server serve up your new Zend Framework application, change into the project directory and run php -S iocaihost:8000 -t public/ pubtic/index.php. Visit http://localhost:8000 to see the default front page of your new application.

Zend Framework organizes related application code into modules. In a big applica­tion, you can create separate modules for separate high-level parts of your program. For this small sample application, we’ll add code to the base Application module that’s already there. This module contains some default routing logic that maps paths under /Application to code in controller classes in a specific place in the filesystem. Example 18-5 shows a new MenuController.php. Save it in the module/Application/src/ Application/Controller directory of your Zend Framework project.

Example 18-5. A Zend Framework controller

namespace Apptication\Controtter;

use Zend\Mvc\Controtter\AbstractActionControtter;

use Zend\View\Modet\ViewModet;

class MenuController extends AbstractActionControtter {

public function showAction()

{

$now = new \DateTime();

$items = [ “Fried Potatoes”, “Boited Potatoes”, “Baked Potatoes” ];

return new ViewModet(array(‘when’ => $now, ‘what’ => $items));

}

}

Then, to tell the framework about your new class, find the section of module/Applica- tion/config/module.config.php that looks like this:

‘controtters’ => array(

‘invokabtes’ => array(

‘Apptication\Controtter\Index’ =>

‘Apptication\Controtter\IndexControtter’

),

),

And add this line as a second element in the invokabtes array:

‘Apptication\Controtter\Menu’ => ‘Apptication\Controtter\MenuControtter’

Don’t forget to add a comma after ‘Application\Controller\IndexController’ so that the array elements have the proper syntax. When you’re done, this section of the config file should look like this:

‘controllers’ => array(

‘invokables’ => array(

‘Application\Controller\Index’ =>

‘Application\Controller\IndexController’,

‘Application\Controller\Menu’ =>

‘Application\Controller\MenuController’

),

),

Now you’ve got a new controller and the framework knows how to use it. The last step is to add a view so the time and items information can be rendered. With Zend Framework, the default template language is just plain PHP. Save the code in Example 18-6 in module/Application/view/application/menu/show.phtml under your project directory.

Example 18-6. A Zend Framework view

<p> At <?php echo $when->format(“g:i a”) ?>, here is what’s available: </p>

<ul>

<?php foreach ($what as $item) { ?>

<li><?php echo $this->escapeHtml($item) ?></li>

<?php } ?>

</ul>

The keys in the array passed to new ViewModel() in the controller are local variable names in the view. This makes accessing these values very straightforward. However, because the template language is plain PHP, HTML entities and other special charac­ters are not escaped by default. Example 18-6 uses the escapeHtml() helper method to escape special characters in each item name.

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *