【问题标题】:Handle CodeIgniter remapping with parameters使用参数处理 CodeIgniter 重新映射
【发布时间】:2017-12-04 23:14:22
【问题描述】:

我将以下代码添加到我的所有控制器正在扩展的 MY_Controller 中:

public function _remap($method, $params = array())
    {//exit($this->router->fetch_class());
        if (array_search($method, $this->private_methods) !== false && !$this->logged_in)
        {
            $this->session->set_flashdata('message', array(
                                                        'message'   =>  'You must login to access the requested area',
                                                        'error'     =>  1
                                                        )
                                        );
            redirect('/');
        }
        else if (method_exists($this, $method))
        {
            $this->$method($params);
        }
        else
        {
            redirect('/');
        }

    }

正在创建的问题是调用 $this->$method($params) 正在将参数压缩到一个数组中。所以像下面这样的方法会中断:

function some_method($param1, $param2, $param3)

有没有办法将这个数组分解为类似这样的函数的单个项目?

【问题讨论】:

  • 那是我的帖子,上面的代码就是基于那个代码。您所指的链接中的代码不会分隔参数,而是将它们作为数组发送。
  • 你可能做错了什么。我进行了测试,它正在发送单个参数。创建Example 控制器并测试答案。

标签: codeigniter remap


【解决方案1】:

我试图做同样的事情并发现同样的问题

$this->$method($params);

我找到了另一个选项

call_user_method_array($method,$this,$params);

有效但已弃用。 Function in PHP deprecated, what should I use now?

但希望这是新的。

call_user_func_array(array($this,$method), $params);

【讨论】:

    【解决方案2】:

    我已经试过了,它对我有用

    public function _remap($method,$params = array())
      {
        if (method_exists($this, $method)) {
    
          if($method == 'delete_photo' || $method == 'delete_video'){
    
            call_user_func_array(array($this,$method), $params);
            }
    
          else{
            $this->$method();
            }
          }
          else {
          $this->index($method);
        }
      }
    

    现在您只需将“delete_photo”和“delete_video”替换为包含参数的方法名称,当然您可以添加任意数量的方法。

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 2015-06-08
      • 2019-12-12
      • 1970-01-01
      相关资源
      最近更新 更多