Zendframework 2 tutorial part 3

posted in: Zend Framework | 0

Summary:

5.Routing

The mapping of a URL to a particular action is done using routes that are defined in the module’s module.config.php file. We will add a route for our user actions. This is the updated module config file with the new code highlighted.

<?php

return array(

‘controllers’ => array(

‘invokables’ => array(

‘User\Controller\User’ => ‘User\Controller\UserController’,

),

),

// The following section is new and should be added to your file

‘router’ => array(

‘routes’ => array(

‘user’ => array(

‘type’    => ‘segment’,

‘options’ => array(

‘route’    => ‘/user[/][:action][/:id]’,

‘constraints’ => array(

‘action’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,

‘id’     => ‘[0-9]+’,

),

‘defaults’ => array(

‘controller’ => ‘User\Controller\User’,

‘action’     => ‘index’,

),

),

),

),

),

‘view_manager’ => array(

‘template_path_stack’ => array(

‘user’ => __DIR__ . ‘/../view’,

),

),

);

?>

In this case, the route is “/user[/:action][/:id]“ which will match any URL that starts with /user. The next segment will be an optional action name, and then finally the next segment will be mapped to an optional id. The square brackets indicate that a segment is optional. The constraints section allows us to ensure that the characters within a segment are as expected, so we have limited actions to starting with a letter and then subsequent characters only being alphanumeric, underscore or hyphen. We also limit the id to a number.

Now this route allows us to have the following urls

URL                                        Page                                                    Action

/user                                         Home                                                  index

/user/add                                 Add new user                                     add

/user/edit/1                              Eidt user with id 1                              edit

/user/delete/1                           Delete user with id 1                           delete

6.Creating the controllers

We are now ready to set up our controller. The controller is a class that is generally called {Controller name}Controller. Note that {Controller name} must start with a capital letter. This class lives in a file called {Controller name}Controller.php within the Controller directory for the module.

In our case that is module/User/src/User/Controller. Each action is a public method within the controller class that is named {action name}Action. In this case {action name} should start with a lower case letter.

<?php
namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;

use Zend\View\Model\ViewModel;

class UserController extends AbstractActionController

{

    public function indexAction()

    {

    } 

    public function addAction()

    {

    } 

    public function editAction()

    {

    } 

    public function deleteAction()

    {

    }

}

We already informed about the controller to the module in the controller section of module/User/config/module.config.php
These actions won’t work until we set up the views. Now its time to build the views.

7.Build the view scripts

To integrate the view into our application all we need to do is create some view script files. These files will be executed by the DefaultViewStrategy and will be passed any variables or view models that are returned from the controller action method. These view scripts are stored in our module’s views directory within a directory named after the controller.

Create four view scripts(index.phtml,add.phtml,edit.phtml,delete.phtml) with the following content

1.index.phtml    <h1> Welcome to index page </h1>

2.add.phtml    <h1> Welcome to add page </h1>

3.edit.phtml    <h1> Welcome to edit page </h1>

1.delete.phtml    <h1> Welcome to delete page </h1>

Thats all…..

Go to the browser type the urls like this for

index  —- > zendy.local/user

add  —- > zendy.local/user/add

edit  —- > zendy.local/user/edit

delete  —- > zendy.local/user/delete

Previous Post
Next Post

Leave a Reply