Home > Backend Development > PHP Tutorial > PHP framework - thinkPHP get variable in url, not $_GET[] variable?

PHP framework - thinkPHP get variable in url, not $_GET[] variable?

WBOY
Release: 2023-03-02 08:28:02
Original
1206 people have browsed it

When I turn on 'URL_PARAMS_BIND_TYPE'=>1, //Parameter passing mode allows sequential passing of parameters, that is, the URL does not write variable names

Visit

http://localhost/yb1/Home/Get...

Why is there nothing?

How to assign value to $_GET[""] in thinkPHP?

<code>    public function test($id  ,$name )
    {
        echo $_GET["id"] . "-" . $_GET["name"];
    } 
    </code>
Copy after login
Copy after login

Reply content:

When I turn on 'URL_PARAMS_BIND_TYPE'=>1, //Parameter passing mode allows sequential passing of parameters, that is, the URL does not write variable names

Visit

http://localhost/yb1/Home/Get...

Why is there nothing?

How to assign value to $_GET[""] in thinkPHP?

<code>    public function test($id  ,$name )
    {
        echo $_GET["id"] . "-" . $_GET["name"];
    } 
    </code>
Copy after login
Copy after login

At this time, you need to write code to explain these magical problems.
Look down (from ThinkPHPLibraryThinkDispatcher.class.php line 211):

<code>        $depr   =   C('URL_PATHINFO_DEPR');
        $paths  =   explode($depr,trim($_SERVER['PATH_INFO'],$depr));

        if(!defined('BIND_CONTROLLER')) {// 获取控制器
            if(C('CONTROLLER_LEVEL')>1){// 控制器层次
                $_GET[$varController]   =   implode('/',array_slice($paths,0,C('CONTROLLER_LEVEL')));
                $paths  =   array_slice($paths, C('CONTROLLER_LEVEL'));
            }else{
                $_GET[$varController]   =   array_shift($paths);
            }
        }
        // 获取操作
        if(!defined('BIND_ACTION')){
            $_GET[$varAction]  =   array_shift($paths);
        }
        // 解析剩余的URL参数
        $var  =  array();
        if(C('URL_PARAMS_BIND') && 1 == C('URL_PARAMS_BIND_TYPE')){
            // URL参数按顺序绑定变量
            $var    =   $paths;
        }else{
            preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){$var[$match[1]]=strip_tags($match[2]);}, implode('/',$paths));
        }
        $_GET   =  array_merge($var,$_GET);
    }</code>
Copy after login

The code will first cut the path information in the url, which is the Getpost/test/2/xxx part, and in the previous stage of obtaining the controller, Getpost/test will be removed, and finally During the process of URL parameter binding, the paths data will be directly assigned to var, and then merged into the $_GET array. Therefore, there are only values ​​in the $_GET array, and there is no original host. key, because TP does not know which key you want to bind.


As for how to bind, continue to look at the code:
Look down (from ThinkPHPLibraryThinkApp.class.php line 136):

<code>            switch($_SERVER['REQUEST_METHOD']) {
                case 'POST':
                    $vars    =  array_merge($_GET,$_POST);
                    break;
                case 'PUT':
                    parse_str(file_get_contents('php://input'), $vars);
                    break;
                default:
                    $vars  =  $_GET;
            }
            $params =  $method->getParameters();
            $paramsBindType     =   C('URL_PARAMS_BIND_TYPE');
            foreach ($params as $param){
                $name = $param->getName();
                if( 1 == $paramsBindType && !empty($vars) ){
                    $args[] =   array_shift($vars);
                }elseif( 0 == $paramsBindType && isset($vars[$name])){
                    $args[] =   $vars[$name];
                }elseif($param->isDefaultValueAvailable()){
                    $args[] =   $param->getDefaultValue();
                }else{
                    E(L('_PARAM_ERROR_').':'.$name);
                }   
            }
            // 开启绑定参数过滤机制
            if(C('URL_PARAMS_SAFE')){
                $filters     =   C('URL_PARAMS_FILTER')?:C('DEFAULT_FILTER');
                if($filters) {
                    $filters    =   explode(',',$filters);
                    foreach($filters as $filter){
                        $args   =   array_map_recursive($filter,$args); // 参数过滤
                    }
                }                        
            }
            array_walk_recursive($args,'think_filter');
            $method->invokeArgs($module,$args);</code>
Copy after login

The above is the code for binding parameters. When URL_PARAMS_BIND_TYPE is true, it will pop up the path parameters to the parameters of the function in turn, and then use invokeArgs to assign values.

Can’t you do it directly?

<code>$_GET['id'] = 1001;
$_GET['name'] = 'develop';</code>
Copy after login

dump($_REQUEST); Look, there must be a parameter name in front of the value, in the form of /id/23

Example: http://192.168.45.3:8125/home...

<code>array(2) {
  ["id"] => string(2) "23"
  ["_URL_"] => array(4) {
    [0] => string(4) "home"
    [1] => string(5) "index"
    [2] => string(2) "id"
    [3] => string(2) "23"
  }
}</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template