Allowing __call in AppController

Hi.

PHP5 has a __call magic method that is triggered when invoking inaccessible methods in an object context. I tried using this function as a catch-all for my missing actions, but realized that the Dispatcher checks controller classes for the called action and throws a "Missing Method" error when the action is not found, bypassing the __call magic method.

Is there a workaround for this? If not, may I suggest that the Dispatcher check for the __call magic method before throwing the "Missing Method" error?

The specific problem I am facing is as follows:

I am attempting to create an admin interface using CakePHP's admin routing facilities. However, several admin actions, such as "/admin/users/index", are exactly identical to their corresponding non-admin actions i.e. "/users/index". Instead of adding


function admin_index (){
  $this->setAction ("index");
}

for every missing admin action, I tried adding


function __call ($method, $args){
  $method = substr ($method, 6);
  return call_user_func_array (array ($this, $method), $args);
}

in AppController, which does not work since the Dispatcher bypasses the __call magic method. If my suggestion is accepted, then the following would be viable:


function __call ($method, $args){
  $method = strpos ($method, "admin_") === FALSE ? $method : substr ($method, 6);
  if (!method_exists ($this, $method)) $this->cakeError ('missingMethod');
  return call_user_func_array (array ($this, $method), $args);
}

Asked by codexispoetry, on 30/11/09

1 Answer

In your view of admin* method call the 'link' method of HtmlHelper with parameter 'prefix' set to null.

Something like this:


$html->link('Users', array('controller'=>'users', 'action'=>'index', 'prefix'=>null))

Answered by Sergeyon 30/11/09

<< previous next >>

Your Answer

You can use Creole Wiki Syntax to format your text.

Tagged with

Rating

2

Viewed

525 times

Last Activity

on 30/11/09