我正在尝试从我的本地主机中获取 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
| 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 { |
但是当我使用
category() 而不是 $url[1]
它工作正常。这是索引类。
| 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[1] 抛出错误时包含什么值?
- 它包含来自 URL 的类别,我已经使用 print_r 进行了检查
- 您可能应该阅读 stackoverflow.com/a/19309893/727208
两件事:”/” 在作为获取字符串的一部分的 url 参数中是不合法的。你需要用 URL encoding
封装它
EG:
| 1 | http : //localhost/mvc/index.php?url=Index%2Fcategory |
这也是 “$ctlr->$url[1]” 根本没有调用它的函数的事实。例如:任何 “$ctlr->$url[1]” 解析为 ?? category() ??不存在,你需要制作它。
将此添加到您的代码中
| 1 2 3 4 |
function category ( ) { Index tmp = new Index ( ) ; tmp -> category ( ) ; } |
编辑:我刚刚注意到,它比我想象的更愚蠢.. 你的字符串说 Index/category 不是吗?.. 使类方法静态.. (这段代码很可怕顺便说一句,它几乎没有任何设计知识)没有 Index/category 因为你不能在一个类中调用 category 除非它是一个静态方法。
学习编码。
- Index 类中有函数 category() ,但我发现使用 URL 路由访问该函数有困难。希望你能理解。
- 这就是我封装它的原因,错误说它在您的范围内不存在..(所以它不存在!)…相反,存在一个类方法(编辑 also I noticed that it wasn’t a static method )

评论(0)