I don’t quite understand the question, but I suppose they do $_SERVER['PATH_INFO']
or $_SERVER['REQUEST_URI']
, no. htaccess of Laravel/Symfony should not use PATH_INFO because it is so:
RewriteRule ^ index.php [L]
path_info is only generated in php if you do something similar to this:
RewriteRule (*.?) index.php/$1 [L]
In the case PATH_INFO does not work very well on some servers other than Apache, sometimes the result is different, but the REQUEST_URI
works in the same way in Apache, Ngnix, Lighttpd and Iisexpress (I couldn’t test on standard IIS but I believe it’s the same thing).
The PHP structure should look something like this (this is the structure I used in a personal framework, however this simplified, without checking POST, GET, PUT, etc):
class Route
{
private static $currentPath;
private static $routes = array();
public function path()
{
if (self::$currentPath) {
return $currentPath;
}
//Pega o nome do script atual
$sname = $_SERVER['SCRIPT_NAME'];
//Remove a query string da url
$reqUri = empty($_SERVER['REQUEST_URI']) ? null : preg_replace('#\?(.*)$#', '', $_SERVER['REQUEST_URI']);
//Subtrai o nome do script (se necessário)
$pathInfo = substr(urldecode($reqUri), strlen(substr($sname, 0, -9)));
$pathInfo = '/' . ($pathInfo === false ? '' : $pathInfo);
return self::$currentPath = $pathInfo;
}
public function add($path, $controller)
{
self::$routes[$path] = $controller;
}
public function exec()
{
if (empty(self::$routes[$path])) {
return false;
}
return self::$routes[$path];
}
}
There are many ways to "save" such routes in the case of my class I keep in a array
in the same class:
private static $routes = array();
But you can even keep it in separate places, it will depend on the end-use goal.
The use would be something like:
require 'route.php';
Route::add('/', 'ClasseController@indexaction');
Route::add('/blog', 'ClasseController@blogaction');
Route::add('/admin', 'AdminClasse@action');
echo 'Path: ', Route::path(), '<br>', PHP_EOL; //Retorna o PATH equivalente ao PATH_INFO
echo 'Controller: ', Route::exec(), '<br>', PHP_EOL; //Retorna o controler
I can’t say that it works exactly like this in Laravel/Symfony, but this explanation is to understand how to use the $_SERVER['REQUEST_URI']
or PATH_INFO
.
Laravel and the Routes cache
In Laravel there is a cache structure for the routes (which you mentioned me) that after the command (does not work with Anonimas functions):
php artisan route:cache
A file is generated in projeto/bootstrap/cache/routes.php
, it contains a php variable "serialized" (and encoded in Base64) and uses the method setRoutes
to define all routes that are cached pro "Collection", an example cache:
app('router')->setRoutes(
unserialize(base64_decode('Código base64'))
);
This can be reasonably advantageous for production servers.
As far as I know is done a "mapping" between the route, and controller, function or that will be executed, when this route is accessed. I believe some kind of comparison (if nested) is made until I find the definition that matches the required url.
– mau humor
By the level of Laravel’s source code, I doubt it’s really "nested ifs". Even Laravel 5 can cache routes, it makes no sense to be "a lot of ifs"
– Wallace Maxters
Negative? What’s wrong with the question?
– Wallace Maxters
Okay, it was just a guess .... But it doesn’t really matter how he does it, because deep down, in any programming language, the task of taking one path, or another, is always done by way of conditionals, whether in the script itself, or at a low level, perhaps in an operation requested by the interpreter from the OS, executed directly on the processor ... hehe ...
– mau humor