Hossein Shahsahebi
Hossein Shahsahebi

Reputation: 7288

Codeigniter: Send whole POST request to method of another controller

I have a general controller which will get the POST request and decide to call known method of any controller. The controller will be chosen according to request.

Also I need to send whole POST request to chosen method without tampering.


More Description
Getting post request in controller 1, handling the request and decide to call known_method() of controller X | X != 1. Also sending primary request to the method. E.g.

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}");
}  

But because sending $post as parameter as it will send as GET request, may tamper it's data, it's not practical way. Also storing in session and retrieve that in target method is not a good solution.


Question: How I can send this data to my selected target?

Thanks in advance

Upvotes: 5

Views: 5294

Answers (2)

Jackie
Jackie

Reputation: 494

Suggestions, which I think would be cleaner:

1) Let the request handling in library, instead of controller. Call the library accordingly.

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

2) Handle the decision request in the client or js

Redirection won't work as data would be lost or need to be reposted.

Update/comment: Library is not helper or meant to be lite, it is better viewed as business logic. Operations or heavy logic are commonly placed here, and model interaction as well.

Upvotes: 1

Daniel Krom
Daniel Krom

Reputation: 10058

well you can just include the controller inside a controller

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
}

contoller_a:

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

Upvotes: 3

Related Questions