__call () in CakePHP controllers?

is there a feature __call()

available in CakePHP controllers? I have used this feature in Zend Framework.

class UsersController extends AppController {
    function home(){
        /*some action*/
    }

    function __call($m, $p){
        print_r($m);
        print_r($p);
    }
}

      

I get an error:

Missing method in UserController

<?php

class UsersController extends AppController {

 var $name = 'Users';


 function somemethodsnotincontoller() {

 }

}
?>

      

for url site.com/users/somemethodsnotincontoller

+2


source to share


7 replies


As many have pointed out, __call () is PHP5's own magic method for finding calls to class methods that don't exist.

HOWEVER, Cake's core (dispatcher, I think) checks to see if a method exists before calling it, and if it doesn't, it displays the missing method error.



The solution can help you create your own AppError class and handle the catch all method.

There is a limited amount of information in the chefs book in Error handling

+6


source


Yes, but it won't work because CakePHP calls actions via ReflectionMethod

// CakePHP 2.4.3
// Controller.php
public function invokeAction(CakeRequest $request) {
    try {
        $method = new ReflectionMethod($this, $request->params['action']);

      



and methods so called are not handled _call

.

+1


source


Used for what?

The __call () method is a PHP construct that you can use from classes that allow you to "catch" calls to methods that do not explicitly exist in the class.

From PHP.net :

__ call () is triggered when unavailable methods are called on the context object.

So the answer is yes if you are using PHP 5 or higher.

0


source


__ call () is a language construct, so it is available in all php versions that support it.

0


source


__ call () is a PHP magic method, not any particular structure. There is no way to answer this question without some context, since __call () is defined on a specific object and not globally. Since CakePHP advertises the fact that it is php4 compatible and __call () was introduced in php5, I would say no.

I looked at the production branch for models and there is a __ () call method that looks like it is trying to emulate PHP5 __call ().

https://trac.cakephp.org/browser/branches/1.2.xx/cake/libs/model/model.php?rev=4211#L437


Edit (response to comment):

Looking at the basic Cake controller, there is no "catch-all" method in controllers that mimics Zend's __call () implementation. Your alternative for achieving this goal is to set up a route similar to the route of the cake page to catch all actions directed to the controller and send them to a single method.

Cake Trac for basic controller: https://trac.cakephp.org/browser/branches/1.2.xx/cake/libs/controller/controller.php?rev=4211

Routing cake documentation: http://book.cakephp.org/view/46/Routes-Configuration

One example in the documentation I link to looks like something you can play with to accomplish what I said above:

Router::connect(
    '/cooks/:action/*', array('controller' => 'users', 'action' => 'index')
);

      

Regardless of this action, always use an index action.

0


source


In CakePHP 3 you can definitely use it __call

, just make sure the controller defines isAction()

. For example:

public function isAction($action) {
    // To allow all actions to go to __call:
    return TRUE;
}

public function __call($name, $arguments) {
  //** your code called for every undefined action here **/
}

      

0


source


__call

is one of the PHP 5 magic methods (see Method Overloading for more information).

If you are using PHP 5 (and you are, if you are usinf Zend Framework), you can have a method __call

in your classes that is independent of the structure you are working with.

-1


source







All Articles