Этот код работает в php 7.2.7 (MAMP и LAMP), ваш способ динамического вызова функции недопустим, а две ваши переменные пусты. Это не совсем так, как у вас, но вы можете взять логику из этой демонстрации.
Хорошо, я просто предоставляю очень простой пример отражения с отображением URL в класс вместе с функциями. Я делаю структуру папок, как показано ниже:
- Здесь .htaccess используется для перенаправления всего URL-адреса в index.php (если файл не существует).
- index.php включает в себя весь код, который может инициализировать код (на данный момент там было только три файла - uri.php, urlMapping.php и actions.php)
- URI.php - есть функция, предоставляющая такие значения, как basepath, baseurl, uri
- urlMappig.php - позволяет указать, какой URL-адрес попадет в какой класс вместе с методом
- actions.php вызовет динамический класс и функцию (отражение)
теперь посмотрите в код index.php
<?php
include_once('URI.php');
include_once('urlMapping.php');
include_once('actions.php');
?>
Теперь код insise uri.php file -
<?php
// all function should be accessible to all file loaded now
// return full url
function full_url (){
return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
// returns current directory
function directory() {
$parts = explode("/", __DIR__);
return $parts[count($parts)-1];
}
// return base url
function base_url(){
$dir = directory();
return substr(full_url(), 0, strpos(full_url(), $dir)+strlen($dir));
}
// return uri
function uri() {
return substr(full_url(), strlen(base_url())+1);
}
?>
Теперь код в urlMapping.php
Примечание. Имя файла и имя класса должны совпадать с URL-адресом карты в
этот файл, чтобы вы могли вызывать динамические классы и функции на actions.php. Если нет, это не сработает
<?php
// this $urlMap will be accessed in actions.php
$urlMap = [
// here we use only uri so .. https://example.com/login hit LoginController class along with login function, this is just only the mapping
'login' => ['class'=>'LoginController',
'function'=>'login'],
];
?>
Теперь код actions.php
<?php
// if call is not like example.com/ means no uri is there
if(uri()!='')
{
// if your uri exists in route collection or urlmapping collection then make call to your dynamic class and methods
if(array_key_exists(uri(), $urlMap))
{
// include the class file dynamically from controllers folder
include_once('controllers/'.$urlMap[uri()]['class'].'.php');
// making references for dynamic class
$controlerObject = new $urlMap[uri()]['class']();
// call function dynaically from the referece
$controlerObject->{$urlMap[uri()]['function']}();
}
else
{
// you can make 404 page not
echo 'No routing found';
}
}
// call for home page
else
{
include_once('index.html.php');
}
?>
Теперь controllres / класс LoginController.php,
Примечание. Как уже упоминалось выше, имя файла и имя класса, как в urlMapping.php
<?php
class LoginController{
public function login()
{
// .... something your code goes here
echo 'hello from the login function of login controller';
}
//...... other function you can define
}
?>