A free and open-source book on ZF3 for beginners


5.8. Other Route Types

The Hostname, Scheme, and Method route types are used less commonly compared to the route types mentioned previously.

5.8.1. Hostname

The Hostname route type can be used, for example, if you develop a content management system (CMS) 7 engine, which should serve several websites at once, each site using a different sub-domain. In that case you will define the Hostname route as the parent, and nest child routes of other types inside of it:

7) A Content Management System (CMS) is a website allowing for collaborative creating, editing and publishing content (blogs, pages, documents, videos etc.) using a centralized web interface. CMS systems make it possible for non-programmers to perform the website's daily tasks, like content publishing.

'routename' => [
    'type' => Hostname::class,
    'options' => [
        'route' => ':subdomain.yourserver.com',
        'constraints' => [
            'subdomain' => '[a-zA-Z][a-zA-Z0-9_-]*'
        ],
        'defaults' => [        
        ],
    ],
    'child_routes'=>[
        //...
    ],
],

In the example above, in line 1 we define the route which has the Hostname type. The route option (line 4) defines the domain name to match against. The :subdomain is a wildcard, which can take different sub-domain values. The constraints key defines the regular expression this sub-domain parameter must match. The Hostname route will differentiate your domains, so each site will behave differently, depending on the value of the subdomain parameter returned:

// An example of an action that uses parameters returned by 
// Hostname route.
public function someAction() 
{
    // Get the 'subdomain' parameter from the route.
    $subdomain = $this->params()->fromRoute('subdomain', null);
  
    // Use different logic based on sub-domain.
    //...		
		
    // Render the view template.
    return new ViewModel();
}

5.8.2. Scheme

The Scheme route type is useful if you need to handle HTTP and HTTPS 8 protocols in different ways.

8) The HTTPS protocol is typically used for secure connections, like account page or shopping cart page. When you use HTTPS, the request data is tunnelled through Secure Socket Layer (SSL) channel and not available to third parties.

The typical Scheme route configuration is presented below:

'routename' => [
    'type' => Scheme::class,
    'options' => [
        'scheme' => 'https',
        'defaults' => [
            'https' => true,
        ],    
    ],
    'child_routes'=>[
        //...
    ],
],

Above, we define the route of type Scheme. It takes the scheme option, which should be the scheme to match against (like http or https). If the scheme in HTTP request's URL is exactly the same as the scheme option, the route is considered matching. You can use the defaults key to return some parameters on route match. In the example above, the https boolean parameter will be returned.

5.8.3. Method

The Method route type can be used if you need to direct GET and POST requests into different controller's actions. Its typical configuration is presented below:

'routename' => [
    'type' => Method::class,
    'options' => [
        'verb' => 'post',
        'defaults' => [        
        ],
    ],
    'child_routes'=>[
        //...
    ],
],

Above, we define the route which has the Method type. It takes the verb option, which may be the comma-separated list of acceptable HTTP verbs (like get, post, put, etc.)


Top