Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between literal and segment route?

I am working in zend framework 2, I am using segment type for all my routes but I have noticed the use of literal route type in zend skeleton application. What are they?

like image 462
Umair Abid Avatar asked Feb 14 '13 13:02

Umair Abid


2 Answers

I guess what Umair is actually asking is, what the purpose of the literal route is, when the segment route already covers this functionality.

To explain it in a few words; The segment route does a rather complex matching on the input with a generated regex, while the literal route will do a simple string comparison. This makes it a lot faster and it should be preferred when no parameter matching is required.

like image 155
DASPRiD Avatar answered Oct 16 '22 16:10

DASPRiD


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');
like image 28
Josh Avatar answered Oct 16 '22 15:10

Josh