PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 3, 2022

[FIXED] CakePHP - call_user_func_array() looses $this->request->data?

 February 03, 2022     cakephp, php     No comments   

Issue

I need to call Controller-methods dynamically. For this purpose I wrote the following code:

if(isset($method['postParams'])) {
    foreach($method['postParams'] as $index => $param) {
        $this->request->data[ $index ] = $param;
    }
}

App::import('Controller', $method['controller']);
// get result
$method['controller'] = $method['controller'] . 'Controller';
$Controller = new $method['controller']();

try {
    if(count($method['params'])) {
        $varVal = call_user_func_array(array($Controller, $method['method']), $method['params']);
    } else {
        $varVal = call_user_func(array($Controller, $method['method']));
    }
} catch(Exception $e) {
    $varVal = $e;
}
$this->set($varName, $varVal);

Now I debug the function being called and see that $this->request->data is NULL.

How to solve this?


Solution

For the sake of completeness I'll post the answer to this problem for anyone having a similar issue to find the solution more easily.

Instead of:

$this->request->data

Use:

$Controller->request->data

After instanciating the $Controller object.



Answered By - Jonathan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing