Skip to content

Doc.Install Custom Route

linzongshu edited this page Aug 30, 2013 · 3 revisions

Introduction

THE custom route should be well prepared before install module, since the route configuration will be insert into database during install. Certainly, you can add your route file whenever after this module is installed, and then execute module update to change custom route. But it is not recommended to do this because it is not good for SEO.

IN this documentation, I will introduce you the following details:

  1. The custom route already provided
  2. Config route and add route class

Provided route

ARTICLE module provided a route name article, and its priority is set to 100. This route defines the URL of following page.

1. Article Homepage

The URL of this page is: {domain}/a

The available parameters [1] are: * controller: article [2] * action: index

For example, in my module, I acquire the URL by this code:

    $routeName = '.' . Service::getRouteName();
    $url       = $this->url($routeName, array('controller' => 'article', 'action' => 'index'));

And the variable $url will like this /a.

2. All Article List Page

The URL is: {domain}/a/list

The available parameters are: * list: all * p: page number

3. Category Article List Page

The URL is: {domain}/a/list-{category ID or slug}

The available parameters are: * list: category ID or slug * p: page number

4. Tag Related Article List Page

The URL is: {domain}/a/tag-{tag name}

The available parameters are: * tag: tag name * p: page number

5. Article Detail Page

The URL is: {domain}/a/{publish time}/{article ID or slug}

The available parameters are: * id: article ID * slug: article slug * time: publish time

6. All Topic List Page

The URL is: {domain}/a/topic

The available parameters are: * topic: all * p: page number

7. A Topic Homepage

The URL is: {domain}/a/topic/{topic ID or slug}

The available parameters are: * topic: topic ID or slug

8. A Topic Article List Page

The URL is: {domain}/a/topic/list-{topic ID or slug}

The available parameters are: * topic: topic ID or slug * list: all * p: page number

Config and Add Route

BEFORE added route class, you must added a config file under var folder to override the route config provided by article module itself. And module will read the config to check whether user have define route when assemble URL.

CREATING a folder and config file such as:

|- var
    |- article
        |- config
            |- resource.route.php

THEN you can add configuration data as same as route config file do, for example:

return array(
    'article' => array(
        'section'  => 'front',
        'priority' => 100,

        'type'     => 'Extra\\Article\\Route\\MyCustom',
        'options'  => array(
            'structure_delimiter'   => '/',
            'param_delimiter'       => '/',
            'key_value_delimiter'   => '-',
            'route'                 => '/article',
            'defaults'        => array(
                'module'     => 'article',
                'controller' => 'index',
                'action'     => 'index',
            ),
        ),
    ),
);

FINALLY you can add a class file under the following folder to implement route:

|- usr
    |- extra
        |- article
            |- src
                |- Route
                    |- MyCustom.php

AND the header (mainly namespace) of this class is:

namespace Extra\Article\Route;

use Zend\Mvc\Router\Http\RouteMatch;
use Zend\Stdlib\RequestInterface as Request;
use Pi\Mvc\Router\Http\Standard;

class MyCustom extends Standard
{
    ...

    public function match(Request $request, $pathOffset = null)
    {
        ...
        return new RouteMatch($matches, $pathLength);
    }

    public function assemble(array $params = array(), array $options = array())
    {
        ...
    }
}

AFTER all this have done, if the module have been installed, you must update the module at admin section to bring the route into effect.

FOR more information of custom route, you can refer to documentation of Pi:

Pi Custom Route

Notes:

[1] The available parameters means if user want to added custom route, only these parameters are provided for it to assemble URL.

[2] In the case, the controller is key, and article is its value.