Questo è solitamente fatto instradando tutte le richieste di un unico punto di ingresso (un file che esegue codice diverso in base alla richiesta) con una regola come:
# Redirect everything that doesn't match a directory or file to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [L]
Questo file quindi confronta la richiesta ($_SERVER["REQUEST_URI"]
) contro un elenco di percorsi: una mappatura di un modello che corrisponde alla richiesta di un'azione del controllore (nelle applicazioni MVC) o un altro percorso di esecuzione. I framework spesso includono una route che può dedurre il controller e l'azione dalla richiesta stessa, come una rotta di backup.
Un piccolo, esempio semplificato:
<?php
// Define a couple of simple actions
class Home {
public function GET() { return 'Homepage'; }
}
class About {
public function GET() { return 'About page'; }
}
// Mapping of request pattern (URL) to action classes (above)
$routes = array(
'/' => 'Home',
'/about' => 'About'
);
// Match the request to a route (find the first matching URL in routes)
$request = '/' . trim($_SERVER['REQUEST_URI'], '/');
$route = null;
foreach ($routes as $pattern => $class) {
if ($pattern == $request) {
$route = $class;
break;
}
}
// If no route matched, or class for route not found (404)
if (is_null($route) || !class_exists($route)) {
header('HTTP/1.1 404 Not Found');
echo 'Page not found';
exit(1);
}
// If method not found in action class, send a 405 (e.g. Home::POST())
if (!method_exists($route, $_SERVER["REQUEST_METHOD"])) {
header('HTTP/1.1 405 Method not allowed');
echo 'Method not allowed';
exit(1);
}
// Otherwise, return the result of the action
$action = new $route;
$result = call_user_func(array($action, $_SERVER["REQUEST_METHOD"]));
echo $result;
In combinazione con la prima configurazione, questo è un semplice script che vi permetterà di utilizzare gli URL come domain.com/about
. Spero che questo ti aiuti a dare un senso a quello che sta succedendo qui.
Vedere la mia risposta [Come modificare l'aspetto dell'URL da uno script PHP] (http://stackoverflow.com/questions/8392965/how-to-change-appearance-of-url-from-within-a- php-script/8392997 # 8392997) Ciò che la maggior parte del framework fa è redirigere tutte le richieste in un file che gestisce tutto. – Ibu