【问题标题】:Codeigniter: Send whole POST request to method of another controllerCodeigniter:将整个 POST 请求发送到另一个控制器的方法
【发布时间】:2015-08-15 07:55:21
【问题描述】:

我有一个通用控制器,它将获得POST 请求并决定调用任何控制器的已知方法。控制器将根据要求选择。

我还需要将整个POST 请求发送到所选方法而不被篡改。


更多说明
controller 1 中获取post 请求,处理请求并决定调用controller X | X != 1known_method()。还将主要请求发送到该方法。例如。

public function index()
{
    $post = $this->input->post();

    //handling the request and decide to call the following method of another controller

    Controller_X->known_method($post);
    //OR
    redirect("site_url/controller_X/known_method/{$post}");
}  

但是因为发送$post作为参数,它会作为GET请求发送,可能会篡改它的数据,这是不实用的方法。还存储在session 中并在目标方法中检索它不是一个好的解决方案。


问题:如何将这些数据发送到我选择的目标?

提前致谢

【问题讨论】:

    标签: php codeigniter post parameters


    【解决方案1】:

    你可以将控制器包含在控制器中

    if(toIncludeController_a()){
          $this->load->library('../controllers/Controller_a');
          $this->controller_a->myFunction(); //<- this function can also get the post data using $this->input->post
    }
    

    控制器_a:

    public function myFunction(){
         $data = $this->input->post();
    }
    

    【讨论】:

    • 谢谢老兄。没有任何性能问题?导致低速或此类问题?
    • 欢迎您,不,这没有问题,它比重定向时再次加载整个 CI 更精简
    • 再次感谢,我会测试解决方案,成功后将其标记为正确
    • 我这样做$this-&gt;load-&gt;library('../controllers/xxx'),但我得到这个错误:Unable to locate the specified class: Simanaz.php!我也可以直接以site_url/simanaz 的身份访问控制器
    • 我标记了你的答案,你可以为未来的访客附加include 解决方案。谢谢
    【解决方案2】:

    建议,我认为会更干净:

    1) 让请求在库中处理,而不是在控制器中处理。相应地调用库。

    if(request1) {
        $this->load->library('lib1');
        $this->lib1->handle(); // in lib1, retrieve all post without tampering
    } else if(request2) {
        ...and so on
    }
    

    2)在客户端或js中处理决策请求

    重定向不起作用,因为数据会丢失或需要重新发布。

    更新/评论:库不是帮助者或意味着精简,最好将其视为业务逻辑。操作或繁重的逻辑通常放置在这里,模型交互也是如此。

    【讨论】:

    • 这是一个想法,但助手适用于轻量级操作。现在我需要在我的X controllers 中做一些复杂的操作,所以帮助不是重点。此外,如果我必须重定向,数据必须以某种方式附加到会话等重定向。是的,正如你所说,如果没有策略,数据将丢失
    猜你喜欢
    • 1970-01-01
    • 2018-02-22
    • 2015-03-30
    • 2021-12-20
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    相关资源
    最近更新 更多