Recommend this page to a friend! |
Classes of Jorge Castro | PHP Route One | README.md | Download |
|
DownloadRouteOneRoute for PHP It reads the url route and parses the values, so it could be interpreted manually or automatically. Unlikely other libraries, this library does not have dependencies and it is contained in a single class. This library is based in CoC Convention over Configuration. It reduces the boilerplate but it has fixed functionalities. This library does not allow to change the "route" but it covers practically all cases, so it increases the performance and usability while it sacrifices flexibility. This library is also as fast as possible and slim as possible. What it does?Let's say we do the next operation: An user calls the next website http://somedomain.com/Customer/Insert, he wants want to show a form to insert a customer
or
This code calls to the method InsertActionGet (GET), InsertActionPost (POST) or InsertAction (GET/POST) inside the class Customer The method called is written as follow:
What is $id, $idparent and $event?idLet's se we want to Update a Customer number 20, then we could call the next page > http://somedomain.com/Customer/Update/20 where 20 is the "$id" of the customer to edit (it could be a number of a string) idparentAnd what if we want to Update a Customer number 20 of the business APPL > http://somedomain.com/Customer/Update/20/APPL Where APPL is the idparent eventNow, let's say we click on some button or we do some action. It could be captured by the field _event and it is read by the argument $event. This variable could be send via GET or POST. > http://somedomain.com/Customer/Update/20/APPL?_event=click ModuleNow, let's say our system is modular and we have several customers (interna customers, external, etc.)
> http://somedomain.com/Internal/Customer/Update/20/APPL?_event=click Then, the first ramification is the name of the module (Internal) and it calls the class somenamespace\Internal\controller\CustomerController Getting started1) Create a .htaccess file
where test1.php is the file that it will work as router. ?req=$1 is important because the system will read the route from "req"
RoutesAPI route> https://localhost/api/controller/{action}/{id}/{idparent} where https://localhost* is the base (it could be changed on the constructor) apiindicates we are calling an "api". This value could be changed via$this->setPath()* Controller*. It's the controller class to call. Action*. It's the action (method) to call id*. Some unique identifier. idparent*. Some unique identifier (of the parent of object)
WS routeWS is an alternative to API. We could use API/WS or both. The difference is how is it called (/api/ versus /ws/) > https://localhost/ws/controller/{action}/{id}/{idparent} where https://localhost* is the base (it could be changed on the constructor) wsindicates we are calling an "ws". This value could be changed via$this->setPath()* Controller*. It's the controller class to call. Action*. It's the action (method) to call id*. Some unique identifier. idparent*. Some unique identifier (of the parent of object)
Controller routeUnlikely "api" and "ws" route, the controller route doesn't have a prefix in the route. > https://localhost/controller/{action}/{id}/{idparent} where https://localhost* is the base (it could be changed on the constructor) Controller*. It's the controller class to call. Action*. It's the action (method) to call id*. Some unique identifier. idparent*. Some unique identifier (of the parent of object) router.php:
file CustomerController.php:
FRONT routeThe front route (for the front-end) is different than other routes. Syntactically it is distributed on category, subcategory and subsubcategory. > This route is not identified automatically so it must be set in the constructor > https://localhost/category/{subcategory}/{subsubcategory}/{id} where https://localhost* is the base (it could be changed on the constructor) category* The category that we are calling. subcategory*. (optional) The subcategory subsubcategory*. (optional) The sub-subcategory id. Some unique identifier. (id* is always the last element of the chain, so /category/20, category/subc/20 and /category/subc/subc/20 always returns 20). Example: (isModule=false) > http://localhost/Toys/GoodSmileCompany/Nendoroid/Thanos
Example: (isModule=true) > http://localhost/Retail/Toys/GoodSmileCompany/Nendoroid/Thanos
Example: (isModule=false) > http://localhost/Toys/GoodSmileCompany/Thanos
Methods__construct($base='', $forcedType=null, $isModule=false)
getQuery($key,$valueIfNotFound=null)It gets a query value (URL). >Note: This query does not include the values "req","_event" and "_extra" Example:
setQuery($key,$value)It sets a query value Example:
fetch()Fetch the values from the route, and the values are processed. callObject($classStructure='%sController',$throwOnError=true)Call a method inside an object using the current route.
The name of the method is obtained via the current action 1) {nameaction}Action exists then it's called. 2) Otherwise, if $istpostback=false then it calls the method {nameaction}ActionGet 3) Otherwise, if $istpostback=true then it calls the method {nameaction}ActionPost callObjectEx($classStructure, $throwOnError, $method, $methodGet, $methodPost,$argumentsIt creates and object (for example, a Controller object) and calls the method.<br> Note: It is an advanced version of this::callObject()<br> This method uses {} to replace values based in the next variables:<br> | Tag | Description | |------------------|----------------------------------------------------| | {controller} | The name of the controller | | {action} | The current action | | {event} | The current event | | {type} | The current type of path (ws,controller,front,api) | | {module} | The current module (if module is active) | | {id} | The current id | | {idparent} | The current idparent | | {category} | The current category | | {subcategory} | The current subcategory | | {subsubcategory} | The current subsubcategory | <b>Example:</b>
Call a method inside an object using the current route. callFile($fileStructure='%s.php',$throwOnError=true)It calls (include) a php file using the current name of the controller
getCurrentUrl($withoutFilename = true)Returns the current base url without traling space, paremters or queries > <b>Note</b>: this function relies on $_SERVER['SERVER_NAME'] and it could be modified by the end-user getCurrentServer()It returns the current server without trailing slash.
getUrl($extraQuery = '',$includeQuery=false)It gets the (full) url based in the information in the class.
url($module,$controller,$action,$id,$idparent)It builds an url based in custom values
urlFront($module,$category,$subcategory,$subsubcategory,$id)It builds an url (front) based in custom values
alwaysWWW($https = false)If the subdomain is empty or different to www, then it redirect to www.domain.com.<br> <b>Note: It doesn't work with localhost, domain without TLD (netbios) or ip domains. It is on purpose.</b><br> <b>Note: If this code needs to redirect, then it stops the execution of the code. Usually it must be called at the top of the code</b>
alwaysHTTPS()If the page is loaded as http, then it redirects to https. <b>Note: It doesn't work with localhost, domain without TLD (netbios) or ip domains. It is on purpose.</b><br> <b>Note: If this code needs to redirect, then it stops the execution of the code. Usually it must be called at the top of the code</b>
alwaysNakedDomain($https = false)If the subdomain is www (example www.domain.dom) then it redirect to a naked domain domain.dom<br> <b>Note: It doesn't work with localhost, domain without TLD (netbios) or ip domains. It is on purpose.</b><br> <b>Note: If this code needs to redirect, then it stops the execution of the code. Usually, it must be called at the top of the code</b>
fields$isPostBack (field)if true then the form is called as POST (i.e. a submit button). $typeit returns the current type > Also obtained via getType() |type|url expected|description| |----|------------|------------| | api |domain.dom/api/controller/action/id | {module}\api\controller\action\id\{idparent}?_event=event | | ws |domain.dom/ws/controller/action/id | {module}\ws\controller\action\id\{idparent}?_event=event | | controller |domain.dom/controller/action/id | {module}\controller\action\id\{idparent}?_event=event | | front |domain.dom/cat/subcat/subsubcat/id | {module}\category\subcategory\subsubcategory\id?_event=event | Example:
Example:
Changelog
|