I've been thinking for a while now about the best way to solve this and I do not come up with a satisfactory solution.
I would like to exercise strict control over the routes that are introduced in an API that I am designing in my domain.
There are several entry points, for example:
-
When this URL is written I want to offer general information about the
padres
module:http://www.example.com/padres
-
When writing this URL I want to offer specific information about
padre
withid
equal to1
:http://www.example.com/padres/1
-
When this URL is written I want to offer general information about the
colecciones
module:http://www.example.com/padres/colecciones
-
When I write this URL I want to offer specific information about
coleccion
withid
equal to1
:http://www.example.com/padres/colecciones/1
I am capturing what is entered in the URL with this code:
$requestUri=$_SERVER['REQUEST_URI'];
$arrURL = explode("/",trim($requestUri,'/'));
For example, for the last cited URL, the value of $arrURL
would be:
Array
(
[0] => padres
[1] => colecciones
[2] => 1
)
What I would like is to be able to control the content of $arrURL
to, based on it, make the requests of place to the database or raise Exceptions if there are misspelled URLs.
If for example a user types this:
http://www.example.com/padres/colecciones/1/ddhtfgdgj
I would have this array, which I must identify with an invalid request:
Array
(
[0] => padres
[1] => colecciones
[2] => 1
[3] => ddhtfgdgj
)
I have tried to create an array that contains the entry points, for example:
$arrRecursos=array('padres','colecciones');
and trying to verify if these entry points are in the variable $arrURL
, but it is quite tedious, especially detect possible errors that the user can write and identify the type of request that I must execute. p>
I have tried to control that in $arrURL
there are no more than 3 elements (in that case all requests would be invalid), but I find the problem that both in the general request of colecciones
and in the request of a padre
in specific the array would have the same number of elements and I can not find the way to properly manage the resources with the respective number that has been introduced.
I would like to know if there is any easy and effective way to control URLs and launch requests according to their content.
- Note: I would like to do it in pure PHP, without having to resort to a framework for it. I'm in a domain with shared hosting, I do not want to have to load it with frameworks which I also do not know if they would be compatible with my hosting provider.