Home » Zend Framework 2 » Iterale vs Segment Routen

Iterale vs Segment Routen

A literal route seems to be good for one off pages like the basic example below:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/home',
                'defaults' => array(
                    'controller' => 'homeController',
                    'action' => 'index',
                )
            )
        )
    )
)

For those not familiar with segment routes. These are dynamic and pass the url segments into the controller. This example is from the Zend Framework 2 Getting Started Tutorial

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/album[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

The action segment will go to a function in the controller with that name. So a URL like /album/edit/2 will go to the editAction() function in the AlbumController. The id can be accessed a couple of ways in the controller.

$id = $this->params()->fromRoute('id');

or

$id = $this->getEvent()->getRouteMatch()->getParam('id');

Print Friendly

Schreib einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*
*