Documentation
Routing
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependencies injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Cache : So as not to redo the same calculations several times
- Model : How to use DAOs to access databases
- Plugins : How to create your own plugins, and a list of plugins offered by Temma
- Execution flow : How to manage the execution flow, between plugins and the controller
- Helpers : Items offered by Temma to help you in various circumstances
Basic routing
Temma's basic routing is intentionally very simple.
By default, URLs are mapped to the names of the controllers.
When logging into
www.site.com/article/show/123/title
Temma executes the code
Article::show(123, 'title')
and uses the template
article/show.tpl
Routes are used to define "virtual controllers", serving as aliases for real controllers. This is particularly useful for allowing access to controllers under file names (such as the classic robots.txt and sitemap.xml).
This also makes it possible to have the same controller which responds on several different URLs, possibly doing different processing depending on the controller name requested.
Example:
{
"routes": {
"article": "Posts",
"sitemap.xml": "\\App\\Controllers\\Sitemap",
"sitemap.extended.xml": "\\App\\Controllers\\ExtendedSitemap"
}
}
- Line 2: Definition of routes.
-
Line 3: Definition of the article virtual controller, which is an alias of the
Posts controller.
For example, a call to the URL http://www.site.com/article/list will call the list() method of the Posts object. - Line 4: We make sure that a call to the URL http://www.site.com/sitemap.xml results in the execution of the index() method of the \App\Controllers\Sitemap controller.
- Line 5: We make sure that a call to the URL http://www.site.com/sitemap.extended.xml results in the execution of the index() method of the \App\Controllers\ExtendedSitemap controller.
Advanced routing
The \Temma\Plugins\Router plugin allows you to activate more elaborate routing.
It needs to be enabled in the temma.json file as a pre-plugin, and it uses extended configuration to define which controllers/actions to run based on the URL called:
{
"plugins": {
"_pre": [
"\\Temma\\Plugins\\Router"
]
},
"x-router": {
"GET:/articles":
"\\MyApp\\Ctrl\\Cms::list()",
"GET:/articles/[sort:enum:alpha,date,mark]":
"\\MyApp\\Ctrl\\Cms::list($sort)",
"*:/article/[id:int]/[title:string]":
"\\MyApp\\Ctrl\\Cms::show($id, $title)"
}
}
- Line 4: The \Temma\Plugins\Router object is defined as pre-plugin. It will thus take control and will be able to modify the plugins, the controller and the action that will be executed by the framework.
- Line 7: Extended configuration specific to the advanced router.
- Lines 8 and 9: We define that for the URL /articles called in GET, it is necessary to execute the list() method of the \MyApp\Ctrl\Cms object.
- Lines 10 and 11: We define that a GET call to the URL /articles/ can be followed by a parameter (which we will call sort) which can only contain alpha, date or mark values. In this case, Temma will have to execute the list() method of the \MyApp\Ctrl\Cms object, giving it the sort value as a parameter.
- Lines 12 and 13: We define that for the URL /article/, followed by an integer parameter (which will be called id) and a text parameter (named title), Temma must execute the show() method of the \MyApp\Ctrl\Cmsobject, providing it with the id and title values retrieved from the URL as parameters. This route works regardless of the method used (GET, POST, PUT…).
In addition to defining the action to be performed for each route, it is also possible to define the pre-plugins and post-plugins to be called.
{
"x-router": {
"GET:/articles":
"\\MyApp\\Ctrl\\Cms::list()",
"GET:/articles/[sort:enum:alpha,date,mark]":
"\\MyApp\\Ctrl\\Cms::list($sort)",
"*:/article/[id:int]/[title:string]":
"\\MyApp\\Ctrl\\Cms::show($id, $title)",
"POST:/article": {
"_pre": "\\MyApp\\Plugins\\Auth",
"action": "\\MyApp\\Ctrl\\Cms::create()",
"_post": [
"\\MyApp\\Plugins\\LogFlow",
"\\MyApp\\Plugins\\EmailToAdmin"
],
},
"PUT:/article/[id:int]": {
"_pre": [
"\\MyApp\\Plugins\\Auth",
"\\MyApp\\Plugins\\DataCheck"
],
"action": "\\MyApp\\Ctrl\\Cms::update($id)",
"_post": "\\MyApp\\Plugins\\LogFlow"
},
}
}
-
Line 9: We define a route for the /article URL called in POST.
- Line 10: A pre-plugin is called for this route.
- Line 11: We specify the controller and the action associated to this route.
- Line 12 to 15: We configure two post-plugins to run for this route. If there are several plugins, you have to create a list.
-
Line 17: We define a route for the /article/ URL, followed by an integer parameter
(which we will call id), called with the PUT method.
- Line 18 to 21: There are two pre-plugins for this route.
- Line 22: We specify the controller and the action associated to this route.
- Line 23: There is a post-plugin for this route.
Previous: | Configuration |
Next: | Log |
Table of Contents
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependencies injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Cache : So as not to redo the same calculations several times
- Model : How to use DAOs to access databases
- Plugins : How to create your own plugins, and a list of plugins offered by Temma
- Execution flow : How to manage the execution flow, between plugins and the controller
- Helpers : Items offered by Temma to help you in various circumstances