【发布时间】:2021-05-29 11:25:29
【问题描述】:
我正在尝试学习如何使用 alto 路由器以及我想要什么“非常简单”。
示例:
- “/”应该调用“AppController->index()”
- “/profil”应该调用“ProfilController->profil()”
- /profil/1" 应该调用 "ProfilController->profilById()
等等……
这是我迄今为止尝试过的:
<?php
use App\Controller\AppController;
require './vendor/autoload.php';
putenv("BASE_URL=/formulaire-php");
// Router
$router = new AltoRouter();
$router->setBasePath('/formulaire-php');
$router->map('GET', '/', 'AppController#index');
$match = $router->match();
if ($match === false) {
echo "404";
} else {
list($controller, $action) = explode('#', $match['target']);
if (is_callable(array($controller, $action))) {
call_user_func_array(array($controller,$action), array($match['params']));
} else {
// here your routes are wrong.
// Throw an exception in debug, send a 500 error in production
}
}
htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
composer.json
{
"require": {
"altorouter/altorouter": "^2.0"
},
"autoload": {
"psr-4": {
"App\\": "src/controller/"
}
}
}
应用控制器:
namespace App\Controller;
class AppController
{
public function index()
{
echo "I index code + return index view here";
}
现在我完全没有错误,所以很难知道发生了什么..
【问题讨论】:
标签: php altorouter