Skip to content
Basilius Sauter edited this page Mar 9, 2016 · 7 revisions

Definition

Routes are heart of NewLoGD and are connecting "API paths" accessible via an URL with an appropriate controller which delivers the requested content. In the official language, we try to distinguish between a path (the requested URL, so to speak) and the route (which is the connection between the path and the controller). Furthermore, the route not only connects these two parts but also limits the request method - this enables to connect the same path to different controllers based on the request method.

Usage

Routes are defined in the file "app/http/routes.php". We distinguish between static routes matching only one possible path and dynamic routes which matches a whole set of paths defined by placeholder elements.

The request mode is a constant defined in NewLoGD\Application. routes.php defines a shortcut for it. You are advised to only use the constants since their internal values could change at any point. The constants are:

  • A\GET, for standard GET requests;
  • A\POST, for POST requests;
  • A\PUT, for PUT requests;
  • A\DELETE, for DELETE requests.

Static routes

$app->addRoute(A\GET, "/", "MainController@run");

In this example, the application is ordered that all GET requests going to ./ have to be answered by the MainController class (saved in "app/http/controllers/MainController.php") by using the run() method. NewLoGD\Application creates an instance of the MainController supplying it with basic informations (such as a reference to itself as well as to the HttpResponse object) and calls then the ->run() on this method.

MainController->run() returns an array containing some very basic information about the application - version number, game title, whether the user has been authorized or not.

Dynamic routes

$app->addRoute(A\GET, "/user/{id}", "UserController@getUser");

Dynamic routes are an important tool to offer the ability to fetch more informations about certain database entries. They have one or more placeholders that get replaced by the engine with a regular expression matching a defined set of symbols. If a matching path has been found, the controller gets called and the method gets supplied with the informations collected by the regular expression. /user/{id} matches, for example, /user/1 and /user/100 and any other positive integers, but not /user/world.

It is important to note that static routes have priority over dynamic routes. If a route matches a static route as well as a dynamic one, only the static one gets called. This is a feature which could be used to supply the user with static informations.

Possible placeholders include:

  • {id}, matches only positive integers
  • {int}, matches positive and negative integers
  • {word}, matches a word (only letters)
  • {text}, matches a text (containing letters, numbers and spaces, possibly more?)

Placeholders are subject to change and could be extended in the future if there is need.

Adding whole groups of routes

Clone this wiki locally