URL is not working when it's trying it for routing
我正在尝试从我的本地主机中获取 URL,这里是
http://localhost/mvc/index.php?url=Index/category 一切进展顺利,但是当我尝试使用 URL
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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(); } ?> |
但是当我使用
它工作正常。这是索引类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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 参数中是不合法的。你需要用 URL encoding
封装它
EG:
1 | http://localhost/mvc/index.php?url=Index%2Fcategory |
这也是
将此添加到您的代码中
1 2 3 4 | function category() { Index tmp = new Index(); tmp->category(); } |
编辑:我刚刚注意到,它比我想象的更愚蠢.. 你的字符串说
学习编码。