I write a router, I use a regular expression. On exit, preg_match returns false. routes.php
<?php use test_shop\Router; Router::add('^$', ['controller' => 'Main', 'action' => 'index']); Router::add('^(?P<controller>[az-])+/?(?<action>[az-]+)?$'); Router.php
<?php namespace test_shop; class Router { protected static $routes = []; protected static $route = []; public static function add($regexp, $route = []) { self::$routes[$regexp] = $route; } public static function getRoutes() { return self::$routes; } public static function getRoute() { return self::$route; } public static function dispatch($url) { if (self::matchRoute($url)){ echo 'OK'; }else { echo 'NO'; } } public static function matchRoute($url) { foreach (self::$routes as $pattern => $route) { if (preg_match("#{$pattern}#", $url,$matches)) { foreach ($matches as $k => $v) { if (is_string($k)) { $route[$k] = $v; } } if (empty($route['action'])){ $route['action'] = 'index'; } debug($route); return true; } } return false; } } The output http://localhost/test_shop/ gives NO
PHP version 7.0
If you write if(!preg_match .... then At the output http://localhost/test_shop/ gives
Array ( [controller] => Main [action] => index ) OK If the url to specify the next path, the value in the array does not change
class App { public static $app; public function __construct() { $query = trim($_SERVER['QUERY_STRING'], '/'); session_start(); self::$app = Registry::instance(); $this->getParams(); new ErrorHandler(); Router::dispatch($query); } protected function getParams(){ $params = require_once CONFIG . '/params.php'; if(!empty($params)) { foreach ($params as $k => $v) { self::$app->setProperty($k,$v); } } } }
http://localhost/test_shop/, then remove it from$url, and then check for matches with the regular account - Visman[controller] => page[action] => indexarray, now by this link it returns the[controller] => Mainarray[controller] => Main[action] => indexempty array and NO - Vladimirs Alesejevs