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?
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With