class Route
{
private $path;
private $method;
private $callback;
public function __construct($uri, $method, $callback)
{
$this->path = $uri;
$this->method = $method;
$this->callback = $callback;
}
private function prepareCallback($callback, $uri)
{
return call_user_func($callback);
}
private function prepareController($callback)
{
if (strstr($callback, '@') == true) {
list($classController, $method) = explode('@', $callback);
$controller = new $classController;
return $controller->$method();
}
throw new \Exception('Неверно указан вызов контроллера. Укажите: Полное\Имя\Класса@названиеМетода');
}
public function getPath()
{
return $this->path;
}
public function match($method, $uri): bool
{
if ($this->method === $method AND $this->path === $uri) {
return true;
}
return false;
}
public function run($uri)
{
if (is_callable($this->callback)) {
return $this->prepareCallback($this->callback, $uri);
}
if (is_string($this->callback)) {
return $this->prepareController($this->callback);
}
throw new \Exception('метод run() не смог обработать action');
}
}
spl_autoload_register(function ($className)
{
$base_dir = $_SERVER['DOCUMENT_ROOT'] . '/';
$file = $base_dir . str_replace('\\', '/', $className) . ".php";
if (file_exists($file)) {
require $file;
}
});
CREATE TABLE `products` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(250) NOT NULL,
`description` text,
`price` decimal(10,2) NOT NULL,
`img` varchar(250) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb3
implode("','", array_keys($products))