Apache fixes

main
Yasen Pramatarov 2024-08-14 10:11:36 +03:00
parent 1df19d9609
commit e84c880289
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?php
class Router {
private $routes = [];
public function add() {
$this->routes[$pattern] = $callback;
}
public function dispatch($url) {
// remove variables from url
$url = strtok($url, '?');
foreach ($this->routes as $pattern => $callback) {
if (preg_match('#^' . $pattern . '$#', $url, $matches)) {
// move any exact match
array_shift($matches);
return $this->invoke($callback, $matches);
}
}
// if there was no match at all, return 404
http_response_code(404);
echo '404 page not found';
}
private function invoke($callback, $params) {
list($controllerName, $methodName) = explode('@', $callback);
// $controllerClass = "\\App\\Controllers\\$controllerName";
$controllerClass = "../pages/$pageName";
$controller = new $controllerClass();
call_user_func_array([$controller, $methodName], $params);
}
}
?>

14
doc/.htaccess 100644
View File

@ -0,0 +1,14 @@
RewriteEngine On
# limit access to .htaccess
<Files .htaccess>
Order Allow,Deny
Deny from all
</Files>
# don't rewrite CSS, JS, etc.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# all other go to index.php
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

View File

@ -4,4 +4,9 @@
CustomLog \${APACHE_LOG_DIR}/jilo-web_access.log combined CustomLog \${APACHE_LOG_DIR}/jilo-web_access.log combined
ErrorLog \${APACHE_LOG_DIR}/jilo-web_error.log ErrorLog \${APACHE_LOG_DIR}/jilo-web_error.log
<Directory $INSTALL_DIR>
AllowOverride All
</Directory>
</VirtualHost> </VirtualHost>