Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义Route中setParam的bug #496

Open
lilj opened this issue Jun 14, 2020 · 1 comment
Open

自定义Route中setParam的bug #496

lilj opened this issue Jun 14, 2020 · 1 comment
Assignees

Comments

@lilj
Copy link

lilj commented Jun 14, 2020

在自定义路由setParam中,如果key是数字的话,在controller中获取到的数组的key不正确。
如果key是字母就没这个问题。

因为之前自定义了路由
http://mydomain.com/controller/action/arg1/arg2/arg3
这样的格式。

php 7.4.7, yaf 3.2.5

--TEST--
Test route and params;
--SKIPIF--
<?php if (!extension_loaded("yaf")) print "skip"; ?>
--INI--
yaf.use_spl_autoload=0
yaf.use_namespace=0

--FILE--
<?php
require "build.inc";
startup();

// Bootstrap
file_put_contents(APPLICATION_PATH . "/Bootstrap.php", <<<PHP
<?php

final class Test_Route implements Yaf_Route_Interface{

  public function route(\$request){

    \$request->setModuleName('Index');
    \$request->setControllerName('Index');
    \$request->setActionName('index');

    // 这里如果key是字母则无问题。
    \$params = [
      '0' => 111,
      '1' => 222,
    ];

    if(!empty(\$params)){
      foreach(\$params as \$k => \$v)
        \$request->setParam((string)\$k, \$v);
    }

    \$request->setRouted(true);
    return true;
  }

  public function assemble(array \$info, array \$query = NULL){}
}

final class Bootstrap extends Yaf_Bootstrap_Abstract {
  public function _init(Yaf_Dispatcher \$dispatcher) {
    \$dispatcher->getRouter()->addRoute('test', new Test_Route);
  }
}
PHP
);

// Controller
file_put_contents(APPLICATION_PATH . "/controllers/Index.php", <<<PHP
<?php
class Index_Controller extends Yaf_Controller_Abstract{
  public function indexAction() {
    //echo PHP_VERSION, PHP_EOL;
    \$params = \$this->getRequest()->getParams();

    echo 'params:', PHP_EOL;
    print_r(\$params);
    echo 'keys:', PHP_EOL;
    print_r(array_keys(\$params));
    echo 'is_array:', (is_array(\$params) ? 'yes': 'no'), PHP_EOL;
    echo 'isset params[1]:', (isset(\$params[1]) ? 'yes': 'no'), PHP_EOL;  //NOT SET
    echo 'array_key_exists:', (array_key_exists(1, \$params) ? 'yes': 'no'), PHP_EOL; // NOT EXISTS

    \$params = [
      0 => 111,
      1 => 222,
    ];

    echo 'params:', PHP_EOL;
    print_r(\$params);
    echo 'keys:', PHP_EOL;
    print_r(array_keys(\$params));
    echo 'is_array:', (is_array(\$params) ? 'yes': 'no'), PHP_EOL;
    echo 'isset params[1]:', (isset(\$params[1]) ? 'yes': 'no'), PHP_EOL;
    echo 'array_key_exists:', (array_key_exists(1, \$params) ? 'yes': 'no'), PHP_EOL;

    exit;
  }
}
PHP
);

$app = new Yaf_Application($config);
$app->bootstrap()
    ->run();

?>
--CLEAN--
<?php
require "build.inc";
shutdown();
?>
--EXPECTF--
params:
Array
(
    [0] => 111
    [1] => 222
)
keys:
Array
(
    [0] => 0
    [1] => 1
)
is_array:yes
isset params[1]:yes
array_key_exists:yes
params:
Array
(
    [0] => 111
    [1] => 222
)
keys:
Array
(
    [0] => 0
    [1] => 1
)
is_array:yes
isset params[1]:yes
array_key_exists:yes
@laruence laruence self-assigned this Jun 15, 2020
@lilj
Copy link
Author

lilj commented Jun 15, 2020

在3.1.4下,$request->setParam("1", "bbb"); 也是如此。

但是之前都是使用

$request->setParam([
   0=>'aaa',  
   1=>'bbb'
]);

不会有这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants