Свободное общение | Router
VaDoSiQ , спасибо))
DELETED
22 июля 2019, в 13:16
Delete
<?php
namespace core;
class Router
{
protected static $routes = [];
protected static $route = [];
protected static function upper($name)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $name)));
}
protected static function match($url)
{
foreach (self::$routes as $pattern => $route) {
if (preg_match("#{$pattern}#i", $url, $matches)) {
foreach ($matches as $k => $v) {
if (is_string($k)) {
$route[$k] = $v;
}
}
if (!isset($route['action'])) {
$route['action'] = 'index';
}
self::$route['controller'] = self::upper($route['controller']);
self::$route['action'] = lcfirst(self::upper($route['action']));
return true;
}
}
return false;
}
public static function add($regex, $route = [])
{
self::$routes[$regex] = $route;
}
public static function dispatch($url)
{
if (self::match($url)) {
$controller = 'app\controllers\\' . self::$route['controller'] . 'Controller';
if (class_exists($controller)) {
$cObj = new $controller(self::$route);
$action = self::$route['action'] . 'Action';
if (method_exists($controller, $action)) {
$cObj->$action();
} else {
echo "Метод {$controller}::{$action} не найден";
}
} else {
echo "Контроллер {$controller} не найден";
}
} else {
http_response_code(404);
require_once WWW . '/404.html';
}
}
}
применение
<?php
error_reporting(E_ALL);
use core\Router;
define('WWW', __DIR__);
define('ROOT', dirname(__DIR__));
define('APP', dirname(__DIR__) . '/app');
spl_autoload_register(function ($class) {
$file = ROOT . '/' . str_replace('\\', '/', $class) . '.php';
if (is_file($file)) {
require_once $file;
}
});
Router::add('^$', ['controller' => 'Main']);
Router::add('^(?P<controller>[a-z-]+)/?(?P<action>[a-z-]+)?$');
Router::dispatch(rtrim($_SERVER['QUERY_STRING'], '/'));
а то развели тут дичь >:o
Вероломство , не плох
Вероломство (22.07.2019 в 13:16)
Вот годный роутер
применение
а то развели тут дичь >:o
Вот годный роутер
<?php
namespace core;
class Router
{
protected static $routes = [];
protected static $route = [];
protected static function upper($name)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $name)));
}
protected static function match($url)
{
foreach (self::$routes as $pattern => $route) {
if (preg_match("#{$pattern}#i", $url, $matches)) {
foreach ($matches as $k => $v) {
if (is_string($k)) {
$route[$k] = $v;
}
}
if (!isset($route['action'])) {
$route['action'] = 'index';
}
self::$route['controller'] = self::upper($route['controller']);
self::$route['action'] = lcfirst(self::upper($route['action']));
return true;
}
}
return false;
}
public static function add($regex, $route = [])
{
self::$routes[$regex] = $route;
}
public static function dispatch($url)
{
if (self::match($url)) {
$controller = 'appcontrollers\' . self::$route['controller'] . 'Controller';
if (class_exists($controller)) {
$cObj = new $controller(self::$route);
$action = self::$route['action'] . 'Action';
if (method_exists($controller, $action)) {
$cObj->$action();
} else {
echo "Метод {$controller}::{$action} не найден";
}
} else {
echo "Контроллер {$controller} не найден";
}
} else {
http_response_code(404);
require_once WWW . '/404.html';
}
}
}
применение
<?php
error_reporting(E_ALL);
use coreRouter;
define('WWW', __DIR__);
define('ROOT', dirname(__DIR__));
define('APP', dirname(__DIR__) . '/app');
spl_autoload_register(function ($class) {
$file = ROOT . '/' . str_replace('\', '/', $class) . '.php';
if (is_file($file)) {
require_once $file;
}
});
Router::add('^$', ['controller' => 'Main']);
Router::add('^(?P<controller>[a-z-]+)/?(?P<action>[a-z-]+)?$');
Router::dispatch(rtrim($_SERVER['QUERY_STRING'], '/'));
а то развели тут дичь >:o
хэ спс норм роутинг) Если ты не заметил я пишу роутер для обычного скрипта) без класс и ООП)
Может кому будет нужен https://github.com/bramus/router
Вероломство (22.07.2019 в 13:16)
Вот годный роутер
применение
а то развели тут дичь >:o
Вот годный роутер
<?php
namespace core;
class Router
{
protected static $routes = [];
protected static $route = [];
protected static function upper($name)
{
return str_replace(' ', '', ucwords(str_replace('-', ' ', $name)));
}
protected static function match($url)
{
foreach (self::$routes as $pattern => $route) {
if (preg_match("#{$pattern}#i", $url, $matches)) {
foreach ($matches as $k => $v) {
if (is_string($k)) {
$route[$k] = $v;
}
}
if (!isset($route['action'])) {
$route['action'] = 'index';
}
self::$route['controller'] = self::upper($route['controller']);
self::$route['action'] = lcfirst(self::upper($route['action']));
return true;
}
}
return false;
}
public static function add($regex, $route = [])
{
self::$routes[$regex] = $route;
}
public static function dispatch($url)
{
if (self::match($url)) {
$controller = 'appcontrollers\' . self::$route['controller'] . 'Controller';
if (class_exists($controller)) {
$cObj = new $controller(self::$route);
$action = self::$route['action'] . 'Action';
if (method_exists($controller, $action)) {
$cObj->$action();
} else {
echo "Метод {$controller}::{$action} не найден";
}
} else {
echo "Контроллер {$controller} не найден";
}
} else {
http_response_code(404);
require_once WWW . '/404.html';
}
}
}
применение
<?php
error_reporting(E_ALL);
use coreRouter;
define('WWW', __DIR__);
define('ROOT', dirname(__DIR__));
define('APP', dirname(__DIR__) . '/app');
spl_autoload_register(function ($class) {
$file = ROOT . '/' . str_replace('\', '/', $class) . '.php';
if (is_file($file)) {
require_once $file;
}
});
Router::add('^$', ['controller' => 'Main']);
Router::add('^(?P<controller>[a-z-]+)/?(?P<action>[a-z-]+)?$');
Router::dispatch(rtrim($_SERVER['QUERY_STRING'], '/'));
а то развели тут дичь >:o
Стесняюсь спросить, чем?
Стр.: 1, 2