Skip to main content

Defining a Route Name

You can define route with constant and/or variable. For example:

PatternDescription
/myrouteMatches exactly "/myroute"
/myroute/{id}Matches /myroute + any character combination and set to ID
/myroute/{id:[0-9]+}Matches /myroute + any number combination and set to ID

All variables defined above will be available as a parameter. In the example above, if the route matches the "id" you can get using $request->param('id');

Creating the pattern:

  • {variable} - Match anything and sets to "variable".
  • {variable:specific} - Match only if the value is "specific" and sets to "variable"
  • {variable:[0-9]+} - Match the regex "[0-9]+" and sets to variable;

all matches values can be obtained by

$this->getRequest()->param('variable');

Best Practices for Route Names

  • Use lowercase for all route names.
  • Use hyphen to separate words.
  • Use nouns to define the route name and the method to define the action.
  • Generally, Use plural nouns do the route name.
  • Forwards slashes are used to define a hierarchy of resources.
  • Use query parameters to filter the list of items.

e.g.

MethodRoute NameDescription
GET/products/{id}Get a specif product
GET/productsGet a list of product
POST/productsCreate a new product
PUT/productsUpdate a product
DELETE/productsDelete a product
GET/products/{id}/imagesGet the images of a product
GET/products?page=1Get the first page of products