2017 © Pedro Peláez
 

joomla-package router

Joomla Router Package

image

joomla/router

Joomla Router Package

  • Saturday, July 14, 2018
  • by mbabker
  • Repository
  • 9 Watchers
  • 5 Stars
  • 10,330 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 8 Forks
  • 0 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

The Router Package Build Status

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

The standard router

Construction

The standard router optionally takes a Joomla\Input\Input object. If not provided, the router will create a new Input object which imports its data from $_REQUEST., (*2)

<?php
use Joomla\Router\Router;

// Create a default web request router.
$router = new Router;

// Create a router by injecting the input.
$router = new Router($application->getInput());

Adding maps

The purpose of a router is to find a controller based on a routing path. The path could be a URL for a web site, or it could be an end-point for a RESTful web-services API., (*3)

The addMap method is used to map at routing pattern to a controller., (*4)

<?php
$router = new Router;
$router->addMap('/article/:article_id', '\\Acme\\ArticleController')
    ->addMap('/component/*', '\\Acme\\ComponentFrontController');

Matching an exact route.

<?php
$router->addMap('/articles', 'ArticlesController');
$controller = $router->getController('/articles');

In this case there is an exact match between the route and the map. An ArticlesController would be returned by getController., (*5)

Matching any segment with wildcards

<?php
$router->addMap('/articles/*', 'ArticlesController');
$controller = $router->getController('/articles/foo/bar');

In this case, the router will match any route starting with "/articles/". Anything after that initial prefix is ignored and the controller would have to inspect the route manually to determine the last part of the route., (*6)

<?php
$router->addMap('/articles/*/published', 'PublishedController');
$controller = $router->getController('/articles/foo/bar/published');

Wildcards can be used within segments. In the second example if the "/published" suffix is used, a PublishedController will be returned instead of an ArticlesController., (*7)

Matching any segments to named variables

<?php
$router->addMap('/articles/*tags', 'ArticlesController');
$controller = $router->getController('/articles/space,apollo,moon');

A star * followed by a name will store the wildcard match in a variable of that name. In this case, the router will return an ArticlesController but it will inject a variable into the input named tags holding the value of anything that came after the prefix. In this example, tags will be equal to the value "space,apollo,moon"., (*8)

<?php
$controller = $router->getController('/articles/space,apollo,moon/and-stars');

Note, however, all the route after the "/articles/" prefix will be matched. In the second case, tags would equal "space,apollo,moon/and-stars". This could, however, be used to map a category tree, for example:, (*9)

<?php
$controller = $router->getController('/articles/*categories', 'ArticlesController');
$controller = $router->getController('/articles/cat-1/cat-2');

In this case the router would return a ArticlesController where the input was injected with categories with a value of "cat-1/cat-2"., (*10)

If you need to match the star character exactly, back-quote it, for example:, (*11)

<?php
$router->addMap('/articles/\*tags', 'ArticlesTagController');

Matching one segment to a named variable

<?php
$router->addMap('/articles/:article_id', 'ArticleController');
$controller = $router->getController('/articles/1');

A colon : followed by a name will store the value of that segment in a variable of that name. In this case, the router will return an ArticleController injecting article_id into the input with a value of "1"., (*12)

Note that a route of /articles/1/like would not be matched. The following cases would be required to match this type of route:, (*13)

<?php
$router->addMap('/articles/:article_id/like', 'ArticleLikeController');
$router->addMap('/articles/:article_id/*action', 'ArticleActionController');

If you need to match the colon character exactly, back-quote it, for example:, (*14)

<?php
$router->addMap('/articles/\:tags', 'ArticlesTagController');

Installation via Composer

Add "joomla/router": "~1.0" to the require block in your composer.json and then run composer install., (*15)

{
    "require": {
        "joomla/router": "~1.0"
    }
}

Alternatively, you can simply run the following from the command line:, (*16)

composer require joomla/router "~1.0"

The Versions

14/07 2018

dev-2.0-dev

dev-2.0-dev https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0+ GPL-2.0-or-later

The Requires

  • php ~7.0

 

The Development Requires

framework router joomla

30/06 2018

dev-master

9999999-dev https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0+ GPL-2.0-or-later

The Requires

 

The Development Requires

framework router joomla

08/04 2018

dev-use-route-objects

dev-use-route-objects https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0-or-later

The Requires

  • php ~7.0

 

The Development Requires

framework router joomla

15/03 2018

1.2.0

1.2.0.0 https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

framework router joomla

23/08 2014

1.1.2

1.1.2.0 https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

09/02 2014

1.1.1

1.1.1.0 https://github.com/joomla-framework/router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

23/10 2013

1.0

1.0.0.0 https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

23/10 2013

1.1.0

1.1.0.0 https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

22/10 2013

1.0-beta3

1.0.0.0-beta3 https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

16/08 2013

1.0-beta2

1.0.0.0-beta2 https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

16/08 2013

1.0-beta

1.0.0.0-beta https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla

04/06 2013

1.0-alpha

1.0.0.0-alpha https://github.com/joomla/joomla-framework-router

Joomla Router Package

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

framework router joomla