Answer the question
In order to leave comments, you need to log in
Routing issue Notice: Undefined offset?
Here is an excerpt from router.class.php .
And it throws errors
Notice: Undefined offset: 2 in classes/router.php on line 5
Notice: Undefined offset: 3 in classes/router.php on line 9
Notice: Undefined offset: 1 in classes/router.php on line 12
Notice: Undefined offset: 3 in classes/router.php on line 14
Notice: Undefined offset: 4 in classes/router.php on line 15
Notice: Undefined offset: 5 in classes/router.php on line 16
public static function executeAction() {
$module = isset(self::$params[0]) ? self::$params[0]: 'index';
$action = isset(self::$params[1]) ? self::$params[1]: 'default';
$params = self::$params[2];
$_GET['app'] = $action;
$_GET['add'] = $params;
$_GET['param'] = $params;
$_GET['param_2'] = self::$params[3];
$_GET['page_name'] = $module;
$_GET['prm_0'] = isset(self::$params[0]) ? self::$params[0]: 'index';
$_GET['prm_1'] = self::$params[1];
$_GET['prm_2'] = isset(self::$params[2]) ? self::$params[2]: 'default';
$_GET['prm_3'] = self::$params[3];
$_GET['prm_4'] = self::$params[4];
$_GET['prm_5'] = self::$params[5];
if (file_exists('core/modules/' . $_GET['prm_0'] . '/main.php')) {
require_once 'core/modules/' . $_GET['prm_0'] . '/main.php';
} else {
$tpl->content = $eng->msg('2', 'Ошибка, страница не найдена!', '2');
}
}
class router {
public static $routes = array();
private static $params = array();
public static $requestedUrl = '';
public static function addRoute($route, $destination=null) {
if ($destination != null && !is_array($route)) {
$route = array($route => $destination);
}
self::$routes = array_merge(self::$routes, $route);
}
public static function splitUrl($url) {
return preg_split('/\//', $url, -1, PREG_SPLIT_NO_EMPTY);
}
public static function getCurrentUrl() {
return (self::$requestedUrl?:'/');
}
public static function dispatch($requestedUrl = null) {
if ($requestedUrl == null) {
//$uri = reset(explode('?', $_SERVER["REQUEST_URI"]));
$arr = explode('?', $_SERVER["REQUEST_URI"]);
$uri = reset($arr);
$requestedUrl = urldecode(rtrim($uri, '/'));
}
self::$requestedUrl = $requestedUrl;
if (isset(self::$routes[$requestedUrl])) {
self::$params = self::splitUrl(self::$routes[$requestedUrl]);
return self::executeAction();
}
foreach (self::$routes as $route => $uri) {
if (strpos($route, ':') !== false) {
$route = str_replace(':any', '(.+)', str_replace(':num', '([0-9]+)', $route));
}
if (preg_match('#^'.$route.'$#', $requestedUrl)) {
if (strpos($uri, '$') !== false && strpos($route, '(') !== false) {
$uri = preg_replace('#^'.$route.'$#', $uri, $requestedUrl);
}
self::$params = self::splitUrl($uri);
break;
}
}
return self::executeAction();
}
public static function executeAction() {
$module = isset(self::$params[0]) ? self::$params[0]: 'index';
$action = isset(self::$params[1]) ? self::$params[1]: 'default';
$params = self::$params[2];
$_GET['app'] = $action;
$_GET['add'] = $params;
$_GET['param'] = $params;
$_GET['param_2'] = self::$params[3];
$_GET['page_name'] = $module;
$_GET['prm_0'] = isset(self::$params[0]) ? self::$params[0]: 'index';
$_GET['prm_1'] = self::$params[1];
$_GET['prm_2'] = isset(self::$params[2]) ? self::$params[2]: 'default';
$_GET['prm_3'] = self::$params[3];
$_GET['prm_4'] = self::$params[4];
$_GET['prm_5'] = self::$params[5];
if($_GET['prm_0'] == 'admin') {
if (file_exists('app/' . $_GET['prm_1'] . '/main.php')) {
require_once 'app/' . $_GET['prm_1'] . '/main.php';
} else {
$tpl->content = $eng->msg('2', 'Ошибка, страница не найдена!', '2');
}
} else {
if (file_exists('core/modules/' . $_GET['prm_0'] . '/main.php')) {
require_once 'core/modules/' . $_GET['prm_0'] . '/main.php';
} else {
$tpl->content = $eng->msg('2', 'Ошибка, страница не найдена!', '2');
}
}
}
}
Answer the question
In order to leave comments, you need to log in
You have nothing in self::$params, most likely an empty array. Because of this, notices are thrown, which will not lead to a code crash, but which should ideally be fixed. If you have php 7.0+, then in lines like this: $_GET['prm_1'] = self::$params[1]; can be specified like this:
$_GET['prm_1'] = self::$params[1] ?? 'default or false';
If lower, then by analogy with the line: $_GET['prm_0'] = isset(self::$params[0]) ? self::$params[0]: 'index';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question