PHP:简单路由类,询问如何添加多个路由

PHP:简单路由类,询问如何添加多个路由,php,arrays,router,Php,Arrays,Router,我尝试使用一个简单的路由器类(在框架之前学习基础知识,但我认为我使用的示例路由器有问题。下面是我从一位同事那里得到的一个非常小的路由器类,我尝试将它集成到我的代码中,以替代我以前使用echo的地方(注释掉部分代码)。 loginController showLoggedInUser()和registerController index()仅用于呈现html模板 都是$router->add()如果我只使用它来添加一条路由,它会起作用,但是我的路由器不会在数组中保存多条路由,因为似乎每条路由都会保

我尝试使用一个简单的路由器类(在框架之前学习基础知识,但我认为我使用的示例路由器有问题。下面是我从一位同事那里得到的一个非常小的路由器类,我尝试将它集成到我的代码中,以替代我以前使用echo的地方(注释掉部分代码)。 loginController showLoggedInUser()和registerController index()仅用于呈现html模板

都是$router->add()如果我只使用它来添加一条路由,它会起作用,但是我的路由器不会在数组中保存多条路由,因为似乎每条路由都会保存在“/”键下,如果我提供了多条路由,我以前的路由似乎会被简单地覆盖。因此我想我需要调整路由器类。我如何解决这个问题

使用PHP7.4 Router.php

<?php
declare(strict_types=1);

class Router
{
    private array $route;

    public function add(string $url, callable $method): void
    {
        $this->route[$url] = $method;
    }

    public function run()
    {

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        if(!array_key_exists($path, $this->route))

        {
            exit();
        }
        return call_user_func($this->route[$path]);
    }
}

不确定使用相同名称的两个路由的总体原则,但您可以使用每个路由的可调用列表来实现这一点

我已经做了一些更改(包括为每个路由传递的callable)来展示这个原理,但是你应该明白

class Router
{
    private array $route;

    public function add(string $url, callable $method): void
    {
        $this->route[$url][] = $method;
    }

    public function run()
    {

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        if(!array_key_exists($path, $this->route))

        {
            exit();
        }
        foreach ( $this->route[$path] as $paths )   {
            $paths();
        }
        // Not sure what to return in this case.
        // return call_user_func($this->route[$path]);
    }
}

$router = new Router();

// $mysqliConnection = new MysqliConnection();
// $session = new SessionService();

// $loginController = new Login($mysqliConnection);
$router->add('/', function () { echo "login"; } );


// $registerController = new Register($mysqliConnection);
$router->add('/', function () { echo "Register"; });


echo $router->run();

相反,我建议使用单独的url、
/login
/register
,以便可以分别调用它们。

这难道不是路由器所需要的行为吗?不能有两条同名路由。如果有两条路由处理对
/
的请求,那么您希望运行哪一条路由?我想我有办法了这里的g非常错误。我想同时运行这两个。我必须在$router->add then中更改我的URL的名称吗?我尝试做一些事情(可能非常愚蠢),并编写了类似$router->add(“/1/”,[$loginController,'showLoggedInUser'])的内容;这不起作用。因此,基本上我很难理解如何添加多个不同的路由并在脚本中运行所有路由。这对我的理解有很大帮助。我只是不明白为什么我要使用两个URL(或想要使用)。我调整了代码,以一种非常丑陋的方式使用两个URL(如下所示).我想这不是什么意思,对吧?$router->add('/showLoggedInUser/,[$loginController,'showLoggedInUser']);$服务器['REQUEST\u URI']='/showLoggedInUser/';echo$router->run();
class Router
{
    private array $route;

    public function add(string $url, callable $method): void
    {
        $this->route[$url][] = $method;
    }

    public function run()
    {

        $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        if(!array_key_exists($path, $this->route))

        {
            exit();
        }
        foreach ( $this->route[$path] as $paths )   {
            $paths();
        }
        // Not sure what to return in this case.
        // return call_user_func($this->route[$path]);
    }
}

$router = new Router();

// $mysqliConnection = new MysqliConnection();
// $session = new SessionService();

// $loginController = new Login($mysqliConnection);
$router->add('/', function () { echo "login"; } );


// $registerController = new Register($mysqliConnection);
$router->add('/', function () { echo "Register"; });


echo $router->run();