?
And everything after it) are deleted from the page path.|
used to divide the choice from several options, optional independent components of the path are grouped in []
), the example is divided into several lines for convenience, before processing the path is divided into slashes and converted into an array of parts of the original path : [language/] [admin/|api/|cli/] [Module_name [/path [/sub_path [/id1 [/another_subpath [/id2] ] ] ] ] ]
en
, ru
), or take into account the region ( en_gb
, ru_ua
).$Request->admin_path === true
), a request to the API ( $Request->api_path === true
), a request to the CLI interface ( $Request->cli_path === true
), or a regular user page unless explicitly stated.$Request->current_module
.My_blog
module has the pair "My_blog" : " "
in translation "My_blog" : " "
, then you can use _
as the module name, while all the same $Request->current_module === 'My_blog'
.$Request->route
, which can be used by modules, for example, for custom routing.$Request->route_ids
contains elements from $Request->route
, which are integers (it is assumed that they are identifiers), $Request->route_path
contains all $Request->route
elements except integers, and is used as a route inside the module.System/Request/routing_replace/before
event is triggered immediately before determining the page language and allows you to somehow modify the source path as a string, the lowest level manipulations can be performed in this place.System/Request/routing_replace/after
event fires after the formation of $Request->route_ids
and $Request->route_path
, allowing you to correct important parameters after they have been defined by the system. Event::instance()->on( 'System/Request/routing_replace/after', function ($data) { $route_path = []; $route_ids = []; foreach ($data['route'] as $item) { if (preg_match('/([af\d]{8}(?:-[af\d]{4}){3}-[af\d]{12}?)/i', $item)) { $route_ids[] = $item; } else { $route_path[] = $item; } } if ($route_ids) { $data['route_path'] = $route_path; $data['route_ids'] = $route_ids; } } );
{ "admin" : { "about_server" : [], "blocks" : [], "databases" : [], "groups" : [ "_", "permissions" ], "languages" : [], "mail" : [], "modules" : [], "optimization" : [], "permissions" : [ "_", "for_item" ], "security" : [], "site_info" : [], "storages" : [], "system" : [], "themes" : [], "upload" : [], "users" : [ "_", "general", "groups", "permissions" ] }, "blank" : [], "languages" : [], "profile" : [], "profiles" : [], "timezones" : [] }
GET api/System/blank GET api/System/admin/about_server SEARCH_OPTIONS api/System/admin/users SEARCH api/System/admin/users PATCH api/System/admin/users/42 GET api/System/admin/users/42/groups PUT api/System/admin/users/42/permissions
/Blogs
page, and the route structure is configured as follows ( modules/Blogs/index.json
): [ "latest_posts", "section", "post", "tag", "new_post", "edit_post", "drafts", "atom.xml" ]
$Request->route_path === []
, but $App->controller_path === ['index', 'latest_posts']
.index
will be here regardless of the module and configuration, but the latest_posts
depends on the configuration. The fact is that if the page is not an API and not a CLI request, then if you specify an incomplete route, the framework will select the first key from the configuration at each level until it reaches the end deep into the structure. That is, Blogs
similar to Blogs/latest_posts
._
as the first element at the appropriate level.modules/Module_name/api/index.json
): { "_" : [] "comments" : [] }
api/Module_name
similar to api/Module_name/_
. This allows you to create an API with beautiful methods (remember that we have identifiers in a separate array): GET api/Module_name GET api/Module_name/42 POST api/Module_name PUT api/Module_name/42 DELETE api/Module_name/42 GET api/Module_name/42/comments GET api/Module_name/42/comments/13 POST api/Module_name/42/comments PUT api/Module_name/42/comments/13 DELETE api/Module_name/42/comments/13
modules/Module_name/index.json
modules/Module_name/admin/index.json
modules/Module_name/api/index.json
modules/Module_name/cli/index.json
Blogs/latest_posts
page from the example above and the final route is ['index', 'latest_posts']
. modules/Blogs/index.php modules/Blogs/latest_posts.php
cs\modules\Blogs\Controller
class ( modules/Blogs/Controller.php
file) must exist with the following public static methods: cs\modules\Blogs\Controller::index($Request, $Response) : mixed cs\modules\Blogs\Controller::latest_posts($Request, $Response) : mixed
GET api/Module_name/items/42/comments
request.api
will be used here. modules/Module_name/api/index.php modules/Module_name/api/index.get.php modules/Module_name/api/items.php modules/Module_name/api/items.get.php modules/Module_name/api/items/comments.php modules/Module_name/api/items/comments.get.php
cs\modules\Blogs\api\Controller
( modules/Blogs/api/Controller.php
file modules/Blogs/api/Controller.php
must exist with the following public static methods: cs\modules\Blogs\api\Controller::index($Request, $Response) : mixed cs\modules\Blogs\api\Controller::index_get($Request, $Response) : mixed cs\modules\Blogs\api\Controller::items($Request, $Response) : mixed cs\modules\Blogs\api\Controller::items_get($Request, $Response) : mixed cs\modules\Blogs\api\Controller::items_comments($Request, $Response) : mixed cs\modules\Blogs\api\Controller::items_comments_get($Request, $Response) : mixed
$Request
and $Response
are nothing but instances of cs\Request
and cs\Response
.cs\Page::json()
, and for other requests to cs\Page::content()
. public static function items_comments_get () { return []; } // public static function items_comments_get () { Page::instance->json([]); }
cs\modules\Blogs\api\Controller::items_comments()
nor cs\modules\Blogs\api\Controller::items_comments_get()
(or similar files) are cs\modules\Blogs\api\Controller::items_comments_get()
, then:OPTIONS
method handler will be checked, if it exists, it decides what to do with itOPTIONS
method handler, then the automatically generated list of existing methods will be sent in the Allow
header (if the called method was different from OPTIONS
, then the status code will be changed to 501 Not Implemented
)OPTIONS
CLI
is a special method, and instead of the Allow
header, the available methods will be displayed in the console (if the called method was different from the CLI
, then the output status will be changed to 245
( 501 % 256
)).index.php
file and connect the router to taste.index.php
does not require controllers and structure in index.json
, you will index.json
most of the routing system.index
prefix).api/Module_name/items/comments
page, the user rights for permissions will be checked (with a space of group label
): api/Module_name index api/Module_name items api/Module_name items/comments
403 Forbidden
error, while the handlers of the previous levels will not be executed, since the access rights are determined at the stage of final formation of the route, before the handlers are launched.Controller.php
in the target folder and open the corresponding file.Source: https://habr.com/ru/post/307690/
All Articles