Set up modern PHP MVC project structure for GetYourBand platform
- Implemented clean MVC architecture with Router, Controller, and Model base classes - Created database migrations for users, bands, bookings, reviews, and availability - Set up Tailwind CSS with yellow color scheme and modern design - Added Alpine.js for reactive JavaScript components - Configured Vite for asset building and hot module replacement - Created authentication and role-based middleware - Implemented helper functions and configuration system - Added comprehensive README with setup instructions - Configured Apache with proper rewrite rules and security headers - Set up Composer and npm package management with modern dependencies
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class Router
|
||||
{
|
||||
private array $routes = [];
|
||||
private array $middlewareStack = [];
|
||||
|
||||
public function get(string $path, $handler): void
|
||||
{
|
||||
$this->addRoute('GET', $path, $handler);
|
||||
}
|
||||
|
||||
public function post(string $path, $handler): void
|
||||
{
|
||||
$this->addRoute('POST', $path, $handler);
|
||||
}
|
||||
|
||||
public function put(string $path, $handler): void
|
||||
{
|
||||
$this->addRoute('PUT', $path, $handler);
|
||||
}
|
||||
|
||||
public function delete(string $path, $handler): void
|
||||
{
|
||||
$this->addRoute('DELETE', $path, $handler);
|
||||
}
|
||||
|
||||
public function group(array $attributes, callable $callback): void
|
||||
{
|
||||
$previousMiddleware = $this->middlewareStack;
|
||||
|
||||
if (isset($attributes['middleware'])) {
|
||||
$this->middlewareStack = array_merge(
|
||||
$this->middlewareStack,
|
||||
(array) $attributes['middleware']
|
||||
);
|
||||
}
|
||||
|
||||
$callback($this);
|
||||
|
||||
$this->middlewareStack = $previousMiddleware;
|
||||
}
|
||||
|
||||
private function addRoute(string $method, string $path, $handler): void
|
||||
{
|
||||
$this->routes[] = [
|
||||
'method' => $method,
|
||||
'path' => $path,
|
||||
'handler' => $handler,
|
||||
'middleware' => $this->middlewareStack,
|
||||
];
|
||||
}
|
||||
|
||||
public function dispatch(string $requestMethod, string $requestUri): void
|
||||
{
|
||||
$requestUri = parse_url($requestUri, PHP_URL_PATH);
|
||||
|
||||
foreach ($this->routes as $route) {
|
||||
if ($route['method'] !== $requestMethod) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$pattern = $this->convertToPattern($route['path']);
|
||||
|
||||
if (preg_match($pattern, $requestUri, $matches)) {
|
||||
array_shift($matches); // Remove full match
|
||||
|
||||
// Execute middleware
|
||||
foreach ($route['middleware'] as $middleware) {
|
||||
$this->executeMiddleware($middleware);
|
||||
}
|
||||
|
||||
// Execute handler
|
||||
$this->executeHandler($route['handler'], $matches);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 404 Not Found
|
||||
http_response_code(404);
|
||||
echo "404 - Page Not Found";
|
||||
}
|
||||
|
||||
private function convertToPattern(string $path): string
|
||||
{
|
||||
$pattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '([^/]+)', $path);
|
||||
return '#^' . $pattern . '$#';
|
||||
}
|
||||
|
||||
private function executeMiddleware(string $middleware): void
|
||||
{
|
||||
$parts = explode(':', $middleware);
|
||||
$name = $parts[0];
|
||||
$params = $parts[1] ?? null;
|
||||
|
||||
$middlewareClass = "App\\Middleware\\" . ucfirst($name) . "Middleware";
|
||||
|
||||
if (!class_exists($middlewareClass)) {
|
||||
throw new \RuntimeException("Middleware not found: {$middlewareClass}");
|
||||
}
|
||||
|
||||
$instance = new $middlewareClass();
|
||||
$instance->handle($params);
|
||||
}
|
||||
|
||||
private function executeHandler($handler, array $params): void
|
||||
{
|
||||
if (is_array($handler)) {
|
||||
[$class, $method] = $handler;
|
||||
$controller = new $class();
|
||||
call_user_func_array([$controller, $method], $params);
|
||||
} elseif (is_callable($handler)) {
|
||||
call_user_func_array($handler, $params);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user