How to access variable from another controller - CakePHP

6

How do I access variables of another controller ?

For example:

I have controller X , and in action index , I create a variable (or a constant). From controller Y , I want to access the value of the variable (or constant) of controller X .

Is it possible to do this? Remembering that each action of each controller , in my application, will have a variable that should be accessible from another controller .

ExampleX Controller

class ExemploXController 
    {
        $dependencias = array("index","listar");

        public function index()
            {
                echo "Index";
            }
        public function funcaoX()
            {
                echo "Funcão X";
            }
        public function listar()
            {
                echo "listando";
            }
    }

Controller ExampleY

class ExemploYController
    {
        $dependencias = array("index","atualizar");

        public function index()
            {
                //pegar o valor da variável $dependencias da controller ExemploXController
                $dependency[] = ExemploXController->dependencias;
            }
        public function funcaoY()
            {
                echo "Funcão Y";
            }
        public function atualizar()
            {
                echo "atualizar";
            }
    }
    
asked by anonymous 10.02.2014 / 21:46

2 answers

3

Accessing variables from other controllers is completely different from accessing their methods or actions.

CakePHP% s are just common classes, so in theory you could access your attributes and methods in the same way as you would any class by simply loading its file.

One way to do this is by using controllers . See the example documentation itself:

// The same as require('Controller/UsersController.php');
App::import('Controller', 'Users');

// We need to load the class
$Users = new UsersController();

As the documentation itself indicates, this is equivalent to directly using the App::import method. Alternatively, you could create a file with the constants you need and include those files in your code, using wherever you need them.

    
11.02.2014 / 14:40
4

Hello, you can create a requestAction . With it you can use both the same controller and other controllers, but do not forget to create the elemment of cache .

link

    
10.02.2014 / 22:06