Load the controller into another controller in cakephp

im developing a web application using multiple pages, each with its own controller.

The problem is that in a controller created for one page there are some variables that are required for another page (with a different controller).

So I need to load one controller into another.

I did this by adding App :: import ("Controller", "Sections");

$ sections = new sectionsController; $ Sections-> constructClasses ();

for the controller, but that doesn't seem to work.

Perhaps the guys have ideas?

Thnx in advance!

+2


a source to share


3 answers


I think there is some misunderstanding in your mind about the MVC architectural pattern . If you need a bullet for your weapon, just get the bullet itself, and you don't have to buy another gun with it. So I hope you understand that a boot controller is a bad idea.

Also if you want some variables to be available for all controllers like Gaurav Sharma you can use .eg Configure::write()

to store data in application app/config/core.php

configuration



Configure::write('somekey',$someval);

      

Then you can get $someval

on Configure::read('somekey')

in any controller.

+4


a source


You can use any of the methods below to access the variable anywhere in your cakePHP application.
1.) use a config class OR 2.) use session for these variables



0


a source


I'm working on this this morning. I am actually getting the controller name from the database, but I changed it to use variables instead.

$controller_name = "Posts"; // the name of the controller.
$action = "index"; // The action we want to call.

App::import('Controller', $controller_name); 

// Now we need the actual class name of the controller.
$controller_classname = $controller_name . 'Controller'; 

$Controller = new $controller_name;
$Controller->variable_name = "something"; // we can set class variables here too.

// Now invoke the dispatcher so that it will load and initialize everything (like components)
$d = new Dispatcher();
$d->_invoke($Controller, array('pass'=> '', 'action' => $action));

// And exit so it doesn't keep going.
exit(0);

      

I honestly didn't bother to figure out what "pass" means (I accept variables), but it warns about it without it. You also need to explicitly call $this->render

in $action

.

0


a source







All Articles