Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php路由如何处理查询参数?_Php_Url Routing - Fatal编程技术网

php路由如何处理查询参数?

php路由如何处理查询参数?,php,url-routing,Php,Url Routing,我的php项目中有一个路由器类,其工作原理如下: public function dispatch(){ foreach ($this->routes as $url => $action) { if( $url == $_SERVER['REQUEST_URI'] ){ if(is_callable($action)) return $action(); $actionArr = explode('#'

我的php项目中有一个路由器类,其工作原理如下:

public function dispatch(){
     foreach ($this->routes as $url => $action) {

        if( $url == $_SERVER['REQUEST_URI'] ){

            if(is_callable($action)) return $action();

            $actionArr = explode('#', $action);
            $controller = 'My\\system\\controllers\\'.$actionArr[0];
            $method = $actionArr[1];

            return (new $controller)->$method();
        }
    }
}
My\system\classes\Registry::get("Router")->add('/My/admin/','AdminController#index');
我对路线的定义如下:

public function dispatch(){
     foreach ($this->routes as $url => $action) {

        if( $url == $_SERVER['REQUEST_URI'] ){

            if(is_callable($action)) return $action();

            $actionArr = explode('#', $action);
            $controller = 'My\\system\\controllers\\'.$actionArr[0];
            $method = $actionArr[1];

            return (new $controller)->$method();
        }
    }
}
My\system\classes\Registry::get("Router")->add('/My/admin/','AdminController#index');
因此,当调用URL
SERVER/My/admin
时,将调用
AdminController
类的
index
方法

我的问题:如何处理查询字符串?

我想要一个有表格的页面。提交时,表单将被发送到
服务器/My/admin/check
,即
admin
文件夹中的
check.php
页面

我是这样定义路线的

My\system\classes\Registry::get("Router")->add('/My/admin/check','AdminController#check');

当然,由于查询字符串附加到URL,因此找不到URL。如何最好地处理此问题?

在检查
$\u服务器['REQUEST\u URI']
之前,请删除第一个
之后的所有内容(如果有)。使用该值检查它是否与
$url
匹配。像这样简单的事情就可以做到:

$request = $_SERVER['REQUEST_URI'];
if( ($pos = strpos($request, '?')) !== false) $request = substr($request, 0, $pos);
任何需要使用查询参数的控制器都应该能够从
$\u get
,或者在最坏的情况下从
$\u服务器['query\u STRING']
获取查询参数