如何使用中音路由器调用控制器->方法

How to call controller->method with alto router

我正在尝试学习如何使用 alto 路由器以及我想要的“非常简单”。

例子:

等...

这是我目前尝试过的方法:

<?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";
    }

现在我完全没有错误所以很难知道发生了什么..

我找到了解决方案:

解决方案 1:将所有路由放入 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

  "autoload": {
    "psr-4": {
      "App\": "src/"
    }
  }

AppController

namespace App\Controller;

use App\Repository\UserRepository;

class AppController extends AbstractController
{
    public function create()
    {
        echo "hello"
    }

solution2 : 将路由放在 config/routes.php 中并在 index.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 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';
}