I do not understand the question, but I will assume that they use $_SERVER['PATH_INFO']
or $_SERVER['REQUEST_URI']
, in Laravel / Symfony .htaccess you should not use PATH_INFO because it looks like this:
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 PATH_INFO case it does not work very well on some different Apache servers, sometimes the result is different, but REQUEST_URI
works identically on Apache, Ngnix, Lighttpd and IISExpress (I could not test on IIS standard but I believe be the same thing.)
The PHP structure should be something like (this is the structure I used in a personal framework, however this simplified, without 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 case of my class I keep in array
in the same class:
private static $routes = array();
But you can even save in separate places, it will depend on the end use goal.
The usage would look 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 not say that it works exactly this way in Laravel / Symfony, but this explanation is to understand how to use $_SERVER['REQUEST_URI']
or PATH_INFO
.
Laravel and the Routes cache
In Laravel there is a cache structure for the routes (which you mentioned to me) that after the command (does not work with anonymous 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 setRoutes
method to define all routes that are in cache for "Collection", an example of cache :
app('router')->setRoutes(
unserialize(base64_decode('Código base64'))
);
This can be reasonably advantageous for production servers.