Answer the question
In order to leave comments, you need to log in
Creating a php router. Another way to parse url?
Hello dear experts in php and mysql. Share your experience.
I am making a router for my CMS (with one index.php entry point), described such a method for receiving and parsing url requests from users:
// Прием и разбор запросов
public function accept()
{
$url_el = array();
$url = $_SERVER['REQUEST_URI'];
if(preg_match("/[^a-zA-Z0-9\/\-\?\=\.\&\#]|(\?{2,}|\-{2,}|\/{2,}|\={2,})/" , $url))
return false;
$url = parse_url($url);
$url_el['path'] = $url['path'];
if(!empty($url['path']) && $url['path'] != '/')
{
if(preg_match("/^\/([a-z0-9\-]+)\.html$/", $url['path'], $ress))
$url_el['static'] = end($ress);
elseif(preg_match("/^\/([a-z0-9\-]+)\/$/", $url['path'], $ress))
$url_el['category'] = end($ress);
elseif(preg_match("/^\/([a-z0-9\-]+)\/([a-z0-9\-]+)\/$/", $url['path'], $ress))
{
$url_el['category'] = $ress[1];
$url_el['subcategory'] = end($ress);
}
elseif(preg_match("/^\/([a-z0-9\-]+)\/([a-z0-9\-]+)\.html$/", $url['path'], $ress))
{
$url_el['category'] = $ress[1];
$url_el['page'] = end($ress);
}
elseif(preg_match("/^\/([a-z0-9\-]+)\/([a-z0-9\-]+)\/([a-z0-9\-]+)\.html$/", $url['path'], $ress))
{
$url_el['category'] = $ress[1];
$url_el['subcategory'] = $ress[2];
$url_el['page'] = end($ress);
}
else
return false;
}
return $url_el;
}
Answer the question
In order to leave comments, you need to log in
Isn't it easier not to fence bicycles and take some kind of ready-made library? For example https://github.com/nikic/FastRoute
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question