Understanding URLs within the Zend framework

lessonsphpprogrammingtutorialtutorialzend framework

Written by:

Reading Time: 2 minutes

In today’s post, I would want to highlight on URLs within the Zend framework. Whether it is a web API or one with a front end, you always have to configure your routes. With routes, there are two major classes you need to be educated upon. And these are Literals and Segments.

As you may have already guessed, with the Zend framework, routes are built out using either one of these. There are subtle differences between a Segment and a Literal. Knowing when to use one over the other would bring convenience to your ZF (Zend framework) development. It goes without saying that it will help you derive the full benefits from using the Zend framework as a whole.

The difference between Segments and Literals

  1. The most noticeable difference boils down to the Literal being strict in its usage, whilst the Segment is liberal.
  2. Segments can do complex matching on the route with a generated regex, whilst Literals do a straight forward string comparison.
  3. When you need to use a URL that needs neither variables nor parameter matchings then use Literals. However if you need to reuse that same route for various purposes, then use Segments.
  4. Consequently, Literal routes are faster to return its output as compared to Segments, because no parameter matching is required.
  5. Only one possible URL (or address) can be matched against a Literal route. However multiple routes can be matched with a single Segment route.
  6. Different classes are called from the Http module.
  • Segments require that you called the Segment class under the Http module like so

use Zend\Router\Http\Segment;

  • Similarly Literals require that you called the Literal class under the Http module just like below

use Zend\Router\Http\Literal;

When would you use one over the other?

Literals are used when you want the route to be as exactly as you have written out in the module.config.php file.

Segments are used when you want variables passed in your action to replace those you have in the module.config.php file.

Usage

Segments can be written as


'type'    => 'segment',

Or as


'type' => Segment::class,

Literal can be written as


'type'    => 'literal',

Or as


'type' => Literal::class,

In both instances, remember to include the required classes. That is like below;


use Zend\Router\Http\Segment;

use Zend\Router\Http\Literal;

An example with Segments

If we have a controller called WorkController, then we can write out a route like below


'router' => [

'routes' => [

'work' => [

'type'    => Segment::class,

'options' => [

'route' => '/work[/:action[/:id]]',

'constraints' => [

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

'id'     => '[0-9]+',

],

'defaults' => [

'controller' => Controller\WorkController::class,

'action'     => 'index',

],

],

],

],

],

All this means that the action and id parameters, can be replaced by other variables. And this will still keep the route valid.

For example, the route is /work[/:action[/:id]] which will match any URL that starts with /work

The following URLs below are valid:

URL Action Parameters
/work Index None
/work/add Add None
/work/edit/2 Edit 2
/work/delete/4 Delete 4

 

Many more routes can be created for this one segment route.

An example with Literals

If we have a controller called HomeController, then a can write out a route like so


'router' => [

'routes' => [

'home' => [

'type'    => Literal::class,

'options' => [

'route' => '/home/address',

'defaults' => [

'controller' => Controller\HomeController::class,

'action'     => address,

],

],

],

],

],

All this means if that only the action address can be used for this route. This is the only way this route can be kept valid.

The following URLs below are valid:

 

URL Action Parameters
/home Address None

 

 

Thank you for spending time with me. Let me know your thoughts on the post. Keep learning and advancing. Cheers.

Leave a Reply

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

Scroll Up