[英]How to call controller->method with alto router
I'm trying to learn how to use alto router and what I want i'ts "pretty simple".我正在尝试学习如何使用中音路由器以及我想要的“非常简单”。
exemple:例子:
etc... ETC...
This is what I've tried so far:这是我迄今为止尝试过的:
<?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作曲家.json
{
"require": {
"altorouter/altorouter": "^2.0"
},
"autoload": {
"psr-4": {
"App\\": "src/controller/"
}
}
}
AppController:应用控制器:
namespace App\Controller;
class AppController
{
public function index()
{
echo "I index code + return index view here";
}
for now I have no error at all so it's difficult to know what's going on..现在我完全没有错误,所以很难知道发生了什么..
I found the solution:我找到了解决方案:
Solution 1: Put all routes in index.php解决方案1:将所有路由放入index.php
index.php index.php
$router = new AltoRouter();
$router->setBasePath('/formulaire-php');
$router->map('GET', '/', 'AppController#create') //controller#method
$match = $router->match();
if (!$match) {
echo "404";
die;
}
if ($match) {
require_once './src/view/template/header.php';
list($controller, $action) = explode('#', $match['target']);
$controller = 'App\\Controller\\' . $controller;
$controller = new $controller;
if (is_callable(array($controller, $action))) {
call_user_func_array(array($controller,$action), array($match['params']));
}
require_once './src/view/template/footer.php';
}
composer.json作曲家.json
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
AppController应用控制器
namespace App\Controller;
use App\Repository\UserRepository;
class AppController extends AbstractController
{
public function create()
{
echo "hello"
}
solution2: put the routes in a config/routes.php and call it in index.php解决方案2:将路由放入 config/routes.php 并在 index.php 中调用
src/Config/Routes.php src/Config/Routes.php
namespace App\Config;
class Routes
{
//The method getRoutes() will return a array with all routes
public static function getRoutes(): array
{
return [
['GET', '/', 'AppController#create'],
['POST', '/save', 'AppController#save'],
['GET', '/save', 'AppController#save'],
['GET', '/user/[i:id]', 'AppController#test'],
];
}
}
index.php The foreach loop will loop over all routes. index.php foreach 循环将遍历所有路由。
<?php
require './vendor/autoload.php';
use App\Config\Routes;
// Router
$router = new AltoRouter();
$router->setBasePath('/formulaire-php');
foreach (Routes::getRoutes() as $route) {
$router->map(...$route);
}
$match = $router->match();
if (!$match) {
echo "404";
die;
}
if ($match) {
require_once './src/view/template/header.php';
list($controller, $action) = explode('#', $match['target']);
var_dump($controller);
$controller = 'App\\Controller\\' . $controller;
$controller = new $controller;
if (is_callable(array($controller, $action))) {
call_user_func_array(array($controller,$action), array($match['params']));
}
require_once './src/view/template/footer.php';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.