if(!empty($_SERVER['REQUEST_URI'])){ return trim($_SERVER['REQUEST_URI'],'/'); } //возвращает uri foreach ($routes as $controller => $method) { if (preg_match("~$controller~",$uri)){ $internalRoute = preg_replace("~$controller~", $method, $uri); $segments = explode('/',$internalRoute); echo $internalRoute; print_r($segments); echo $uri; } }
Here is an array of routes
'product/([0-9]+)' => 'product/view/$1', 'catalog' => 'catalog/index', '' =>'mainPage/index', 'test' =>'test/view'
When I type test.com/index into the address bar, I get
mainPage/indextmainPage/indexemainPage/indexsmainPage/indextmainPage/index //$internalRoute Array ( [0] => mainPage [1] => indextmainPage [2] => indexemainPage [3] => indexsmainPage [4] => indextmainPage [5] => index ) test/viewArray ( [0] => test [1] => view ) //$segments index //$uri
How to fix that he gave me a string corresponding route? As in the pattern of the routes below.
'product/([0-9]+)' => 'product/view/$1', 'catalog' => 'catalog/index', '' =>'mainPage/index', 'test' =>'test/view'
'' =>'mainPage/index',
corresponds to anything, if I'm not mistaken. Try to specify the line boundary in the form'^$' =>'mainPage/index',
- Visman$uri
output before the loop and display it in the question. - Visman