Why Router strange processes a request of type 'article/$1/$2', in this case 'article/$1' right processed?

Router worked flawlessly, until it took shape on the site, queries such as 'article/$1/$2' and 'woking/$1/$2'.

In the process of generating page right is the desired method, the following error occurs: when attempting to connect to the 'working/1/2' is looking for non-existent action1 in the controller, when attempting to connect to the 'working/3/2' is looking for non-existent action3.

Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'WorkingController' does not have a method 'action3' in C:\OS\OSPanel\domains\mysite.ru\components\Router.php on line 65


Help to understand what it is.

File routes
return array(
 'work' => 'work/view',
 'working/([0-9]+)/([0-9]+)' => 'working/getTestByNumq/$1/$2',
 'job/([0-9]+)' => 'job/getTest/$1',
 'article/([0-9]+)' => 'article/view/$1',
 'course' => 'course/index',
 "=> 'main/index',
);


Router Itself
<?php 

class Router
{
 private $routes;

 public function __construct()
{
 $routesPath = ROOT.'/config/routes.php';
 $this->routes = include($routesPath);
}

 // return the request string

 private function getURI()
{
 if (!empty($_SERVER['REQUEST_URI'])) {
 return trim($_SERVER['REQUEST_URI'], '/');
}
}

 public function run()
{
 //Receive string queries
 $uri = $this->getURI();

 //Check this query in routes.php
 foreach ($this->routes as $uriPattern => $path) {

 // Compare $uriPattern $uri
 if (preg_match("~$uriPattern~", $uri)) {

 //Get the internal path of the external rule
 $internalRoute = preg_replace("~^$uriPattern$~", $path, $uri);


 //Determine which controller and action handles the request and parameters

 $segments = explode('/', $internalRoute);

 $controllerName = array_shift($segments) . 'Controller';
 $controllerName = ucfirst($controllerName);

 $actionName = 'action'.ucfirst(array_shift($segments));

 $parameters = $segments;

 //Connect the controller class file
 $controllerFile = ROOT . '/controllers/' . 
 $controllerName . '.php';

 if (file_exists($controllerFile)) {
include_once($controllerFile);
 } 
 // else {
 // header('HTTP/1.0 404 Not Found');
 // readfile('err404.html');
 // exit();
 // }


 //Create the object to call the method (i.e. action)
 $controllerObject = new $controllerName; 
 try { 
 $result = call_user_func_array(array($controllerObject, $actionName), $parameters); 
 } catch (Exception $e) {
 header('HTTP/1.0 404 Not Found');
readfile('err404.html');
exit();
 } 

 if($result != null){
break;
 } 
}
}
}
}


File WorkingController
include_once ROOT.'/models/Working.php';

class WorkingController{ 

 public static function actionGetTestByNumq($sub, $numq){

 require_once (ROOT. '/views/job/job.php');

 return true;
}
}
March 12th 20 at 08:11
2 answers
March 12th 20 at 08:13
Friend code :)

$segments = explode('/', $internalRoute);

Here put brake and look at further actions step by step.

Read the comments and remembered your kament array of routes of the regular season to write the complex to the simple!!!
March 12th 20 at 08:15
preg_match("~^$uriPattern\$~", $uri)

Find more questions by tags PHP