rsthn / rose-ext-router
Router Extension (Rose)
Installs: 119
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Type:rose-extension
pkg:composer/rsthn/rose-ext-router
Requires
README
This extension adds content routing features to Rose.
composer require rsthn/rose-ext-router
After installation ensure Router is the primary service in your system by editing the Gateway section of your system.conf file and set service=router. Additionally unless you want to use index.php/my-route/ we recommend you enable URL rewriting to make pretty URLs. If using Apache or compatible an .htaccess like the following will do:
RewriteEngine On
RewriteBase /my-project/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Default Operation
Router will use the relative path provided by Gateway (i.e. when using something like index.php/home the relative path is /home) and return the appropriate content using the following rules (in order of precedence, whichever is satisfied first):
-
User is authenticated1 and there is a
rcore/content/home/private.htmlfile, it will be loaded, evaluated withExprand sent to the client. -
User is not authenticated and there is a
rcore/content/home/public.htmlfile, it will be loaded, evaluated withExprand sent to the client. -
When a
rcore/content/home/index.htmlfile exists, it will be loaded, evaluated withExprand sent to the client. -
User is not authenticated and there is only a
rcore/content/home/private.htmlthen Router will redirect to/login. -
Router will redirect to
/404.
1The condition "authenticated user" is determined by checking if the user field of the current session (if any) is set to a non-null value (as set by Sentinel).
Routing to Services or URLs
When a certain route (i.e. /api) should pass control to another Rose service (i.e. wind) or redirect to a URL you can use the Router section of the system.conf file to achieve this, by providing "route=service_name" pairs (where route is a delimiter-less regex), such as:
[Router] /api = service:wind /details/([0-9]+) = service:wind/?f=get-details&id=(1) /external-page = location:https://example.com/contact-us/ /([0-9]+)/home = home/(1)
Service Redirection
-
Using the
service:prefix in the value will cause an internal redirection to the specified service.- i.e.
/api = service:windwill redirect to servicewind.
- i.e.
-
Any path provided in the value will be set as the new relative path.
- i.e.
/api = service:wind/detailswill redirect to servicewindsetting relative path to/details.
- i.e.
-
Any path provided by the client after the route base will be appended to the relative path.
- i.e.
/api = service:wind/detailsbut loaded from client as/api/userswill cause redirection to servicewindwith relative path/details/users.
- i.e.
-
Any query parameters in the value will be merged into the current request parameters.
- i.e.
/api = service:wind/details?b=Band loaded from client as/api/users?a=Awill cause redirection to servicewindwith relative path/details/usersand request parametersa=A&b=B.
- i.e.
Location Redirection
-
Using the
location:prefix in the value will cause thelocationheader to be set in the response HTTP headers and an immediate execution termination.- i.e.
/contact = location:https://example.com/will redirect to URLhttps://example.com/.
- i.e.
-
Any path provided by the client after the route base will be ignored.
- i.e.
/contact = location:https://example.com/and loaded from client as/contact/sub-path/will redirect tohttps://example.com/.
- i.e.
-
Any query parameters specified by the client will be ignored.
- i.e.
/contact = location:https://example.com/?a=Aand loaded from client as/contact/sub-path/?b=Bwill redirect tohttps://example.com/?a=A.
- i.e.
Notes
The value of routes is evaluated using Expr, therefore any formating can be made if needed, and variables from the gateway's request parameter map can be accessed as usual via (gateway.request.<name>).