Accessing another controller’s variable - Cakephp

Asked

Viewed 2,560 times

6

How do I access variables from another controller?

For example:

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

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

Controller Exemplox

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

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

Controller Exemploy

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";
            }
    }
  • Can you give a simple but concrete example? What you are asking for you can probably do otherwise.

  • Okay. I added. How would I do that?

2 answers

4

  • That was the best answer. =)

  • @Igormartins This is a good answer if you want to call a method from another controller. But make a request to recover a constant?

  • @utluiz requestAction does not only serve to call a method from a controler. according to the documentation Este método chama uma ação de um controller de qualquer lugar e retorna os dados da ação requisitada.. Você pode usar o requestAction() para recuperar uma view totalmente renderizada passando 'return' no array de opções: requestAction($url, array('return'));. Not to mention that it will require less memory for using the cache.

  • That’s exactly what I said. But that just doesn’t answer the question.

  • 1

    From the moment he can set a variable in a action of a controller and not need to import a controller whole to be able to rescue the action that he needs, answer yes.

3


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

The controllers 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, simply by uploading the respective file.

One way to do this is by using the App::import. See the example of the 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 using the method directly require. Alternatively, you could create a file with the constants you need and include those files in your code, using where necessary.

  • 1

    That’s exactly what I did now, before coming here. We thought together.Anyway, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.