im developing a web application, using multiple pages, each with their own controller.
The problem now is that there are some variables in a controller, created for one page, tha开发者_开发百科t are required for an other page ( with a different controller).
Therefor i need to load one controller into the other one.
I did this by adding App::import('Controller', 'sections');
$sections= new sectionsController; $sections->constructClasses();
to the controller, but this doens't seem to work..
Maybe u guys have some ideas?
Thnx in advance!
I think there're some misunderstandings in your mind about MVC architectural pattern.If you need some bullet for your gun,just get the bullet itself,and it's not neccessary to get another gun with it.So I hope you understand loading controller is really a bad idea.
Also if you want some variables accessable by all controllers,as Gaurav Sharma
mentioned ,you can use Configure::write()
to store data in the application’s configuration app/config/core.php
.e.g
Configure::write('somekey',$someval);
Then you can get $someval
by Configure::read('somekey')
in any controller.
You can use any one of the methods below to access a variable anywhere in the cakePHP application.
1.) use the configuration class OR
2.) use session for those variables
I've been working on this this morning. I actually get the controller name from a database, but I've 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 figuring out what 'pass' is for (I assume variables), but it throws a warning without it.
You will also need to explicitly call $this->render
in your $action
.
精彩评论