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); } } } } 
  • one
    If the root of your site is http://localhost/test_shop/ , then remove it from $url , and then check for matches with the regular account - Visman
  • one
    And show me how you get the url, in my case, the url does not go the way you want - Nikita Pavlov
  • debug ($ url) gave out index.php - Vladimirs Alesejevs
  • @ NikitaPavlov from above pointed out where the dispatch ($ url) method leads - Vladimirs Alesejevs
  • @Edward passed this argument to false. In browse NO. the goal could be to type in the line for example: localhost / test_shop / page / veiw and debug ($ route) would give me the [controller] => page [action] => index array, now by this link it returns the [controller] => Main array [controller] => Main [action] => index empty array and NO - Vladimirs Alesejevs

0