简体   繁体   English

传递参数时,PHP Altorouter中的异常行为

[英]Unexpected behaviour in PHP Altorouter when passing parameters

Setup 设定

I'm accessing this url: <host>/render/z63034/RBLR/GLZB . 我正在访问以下URL: <host>/render/z63034/RBLR/GLZB

My url pattern is as such: /render/[a:title]/[a:bpFrom]/[a:bpTo] . 我的网址格式如下: /render/[a:title]/[a:bpFrom]/[a:bpTo]

My route gets added like so: 我的路线像这样添加:

$router->map("GET", "/render/[a:title]/[a:bpFrom]/[a:bpTo]", function ($params) { include __DIR__ . "/views/render.php"; }, "render");

The call then looks like this: 然后,呼叫如下所示:

call_user_func_array($match['target'], $match['params']);

In my index.php (where all requests are routed to) a var_dump() of $match['params'] yields the expected: 在我的index.php (所有请求都路由到其中var_dump()$match['params']var_dump()产生了预期的结果:

array(3) {
  ["title"]=>
  string(6) "z63034"
  ["bpFrom"]=>
  string(4) "RBLR"
  ["bpTo"]=>
  string(4) "GLZB"
}

In my render.php (which is included) a var_dump() of $params yields the unexpected 在我的render.php (包括在内var_dump()$paramsvar_dump()产生了意外的结果

string(6) "z63034"

Question

Why is only the first element in the array I'm passing to call_user_func_array actually passed (not as an array, but just as the value itself)? 为什么只有我传递给call_user_func_array的数组中的第一个元素才实际传递(不是作为数组,而是作为值本身)?

Notice that call_user_func_array passes the $params as single parameters, with this I mean, that in your function definition, you have to declare as much parameters as your $params array has. 请注意, call_user_func_array$params作为单个参数传递,这意味着在函数定义中,您必须声明与$params数组一样多的参数。

For example if you call this: 例如,如果您调用此命令:

$params = array('Hello', 'World');
call_user_func_array(array($this,'test'), $params);

And your function definition looks like that 您的函数定义如下所示

 function test($a){
        echo $a; 
        echo $b; 
        echo '<br>';
    }

you will only print 'Hello', so you have to declare your function like this 您将只打印“ Hello”,因此您必须像这样声明函数

function test($a, $b){
        echo $a; 
        echo $b; 
        echo '<br>';
    }

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM