Class Phalcon\Mvc\Router\Group

implements Phalcon\Mvc\Router\GroupInterface

Source on GitHub

Helper class to create a group of routes with common attributes

  1. <?php
  2. $router = new \Phalcon\Mvc\Router();
  3. //Create a group with a common module and controller
  4. $blog = new Group(
  5. [
  6. "module" => "blog",
  7. "controller" => "index",
  8. ]
  9. );
  10. //All the routes start with /blog
  11. $blog->setPrefix("/blog");
  12. //Add a route to the group
  13. $blog->add(
  14. "/save",
  15. [
  16. "action" => "save",
  17. ]
  18. );
  19. //Add another route to the group
  20. $blog->add(
  21. "/edit/{id}",
  22. [
  23. "action" => "edit",
  24. ]
  25. );
  26. //This route maps to a controller different than the default
  27. $blog->add(
  28. "/blog",
  29. [
  30. "controller" => "about",
  31. "action" => "index",
  32. ]
  33. );
  34. //Add the group to the router
  35. $router->mount($blog);

Methods

public __construct ([mixed $paths])

Phalcon\Mvc\Router\Group constructor

public setHostname (mixed $hostname)

Set a hostname restriction for all the routes in the group

public getHostname ()

Returns the hostname restriction

public setPrefix (mixed $prefix)

Set a common uri prefix for all the routes in this group

public getPrefix ()

Returns the common prefix for all the routes

public beforeMatch (mixed $beforeMatch)

Sets a callback that is called if the route is matched. The developer can implement any arbitrary conditions here If the callback returns false the route is treated as not matched

public getBeforeMatch ()

Returns the ‘before match’ callback if any

public setPaths (mixed $paths)

Set common paths for all the routes in the group

public getPaths ()

Returns the common paths defined for this group

public getRoutes ()

Returns the routes added to the group

public add (mixed $pattern, [mixed $paths], [mixed $httpMethods])

Adds a route to the router on any HTTP method

  1. <?php
  2. $router->add("/about", "About::index");

public Phalcon\Mvc\Router\Route addGet (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is GET

public Phalcon\Mvc\Router\Route addPost (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is POST

public Phalcon\Mvc\Router\Route addPut (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is PUT

public Phalcon\Mvc\Router\Route addPatch (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is PATCH

public Phalcon\Mvc\Router\Route addDelete (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is DELETE

public Phalcon\Mvc\Router\Route addOptions (string $pattern, [string/array $paths])

Add a route to the router that only match if the HTTP method is OPTIONS

public Phalcon\Mvc\Router\Route addHead (string $pattern, [string/array $paths])

Adds a route to the router that only match if the HTTP method is HEAD

public clear ()

Removes all the pre-defined routes

protected _addRoute (mixed $pattern, [mixed $paths], [mixed $httpMethods])

Adds a route applying the common attributes