// запрос отправляется на URI /authorize
$.ajax({
'url': '/authorization',
'type': 'POST',
'data': $('#loginForm').serialize(),
'success': function () { alert('success!'); }
});
<?php
function parseUri($rawUri) {
$uri = rtrim($rawUri, '/');
$pos = strpos($rawUri, '?');
if ($pos === false) {
return $uri;
}
return substr($uri, 0, $pos);
}
class Router
{
private $rules = [];
public function addRule($pattern, $method, $action) {
$this->rules[] = [
'action' => $action,
'pattern' => $pattern,
'method' => null
];
}
public function get($pattern, $action) {
$this->addRule($pattern, 'GET', $action);
}
public function post($pattern, $action) {
$this->addRule($pattern, 'POST', $action);
}
public function handleRequest($uri) {
foreach ($this->rules as $rule) {
if (preg_match($rule['pattern'], $uri, $matches) && ($rule['method'] === $_SERVER['REQUEST_METHOD'] || !$rule['method'])) {
foreach ($matches as $key=> $value) {
if (!is_numeric($key)) {
$_GET[$key] = $value;
}
}
call_user_func($rule['action']);
}
}
}
}
$uri = parseUri($_SERVER['REQUEST_URI']);
$router = new Router();
$router->get('|/(?<id>\d+)|ui', function() {
printf('Hi there, i am a GET request handler for entity with id %d', $_GET['id']);
});
$router->post('|/autorize|', function () {
$login = $_POST['login'];
$password = $_POST['password'];
var_dump($login, $password);
});
$router->handleRequest($uri);