URL 在尝试路由时不起作用

URL is not working when it's trying it for routing

我正在尝试从我的本地主机上捕获 URL,就在这里 http://localhost/mvc/index.php?url=Index/category 一切进展顺利,但当我尝试使用 URL /category 时,它显示错误。这是错误

Notice: Array to string conversion in C:\xampp\htdocs\mvc\index.php on line 21

Notice: Undefined property: Index::$Array in C:\xampp\htdocs\mvc\index.php on line 21

Fatal error: Uncaught Error: Function name must be a string in C:\xampp\htdocs\mvc\index.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\mvc\index.php on line 21

<?php
include_once "system/libs/main.php";
include_once "system/libs/Dcontroller.php";
include_once "system/libs/Load.php";
?>
<?php
$url = isset($_GET['url']) ? $_GET['url'] : NULL;
if ($url != NULL) {
    $url = rtrim($url,'/');
    $url = explode("/", filter_var($url,FILTER_SANITIZE_URL));
} else {
    unset($url);
}
if (isset($url[0])){
    include 'app/controllers/'.$url[0].'.php';
    $ctlr = new $url[0]();
    if (isset($url[2])) {
        $ctlr->$url[1]($url[2]);
    } else {
        if (isset($url[1])) {
            $ctlr->$url[1]();  //Here is the line where I'm getting the 
                                 error
        } else {

        }           
    }

}else{
    include 'app/controllers/Index.php';
    $ctlr = new Index();
    $ctlr->home(); 
}   
?>

但是当我使用 category() 而不是 $url[1] 它工作正常。这是索引 class.

<?php
class Index extends Dcontroller
{   
    public function __construct()
    {
        parent::__construct();
    }
    public function home()
    {
        $this->load->view("home");
    }
    public function category()
    {
        $data = array();
        $catModel = $this->load->model("CatModel");
        $data['cat'] = $catModel->catList();
        $this->load->view("category", $data);
    }
}

有两件事很直接:“/”在 url 参数中作为 get 字符串的一部分是不合法的。你需要用URL编码

封装它

EG:

  http://localhost/mvc/index.php?url=Index%2Fcategory

这也是事实 "$ctlr->$url[1]" 根本没有调用它的函数.. 例如:无论 "$ctlr->$url[1]" 解析为 ??category()??不存在,你需要做它。

将此添加到您的代码中

 function category() {
       Index tmp = new Index();
       tmp->category();
 }

编辑: 我刚刚注意到,它比我想象的还要愚蠢.. 你的字符串说 Index/category 不是吗?.. 使 class method static..(这段代码很糟糕,因为它几乎没有显示任何设计知识)没有 Index/category 因为你不能在 [=32 中调用 category =] 除非它是静态方法。

学习编码。