2010-05-19 91 views
2

即时通讯使用多个页面开发Web应用程序,每个页面都有自己的控制器。将控制器加载到cakephp中的其他控制器中

现在的问题是,控制器中有一些变量,为一个页面创建,而另一个页面(使用不同的控制器)则需要这些变量。

因此,我需要加载一个控制器到另一个。

我这样做是通过加入 App :: import('Controller','sections');

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

控制器,但这好好尝试一下似乎工作..

也许能跟大家有什么想法?

Thnx提前!

回答

0

您可以使用以下任何一种方法访问cakePHP应用程序中任何位置的变量。
1)使用configuration class OR
2)使用会话这些变量

+0

对不起,但我似乎并没有工作.. – john 2010-05-19 09:46:52

4

我觉得是在你的心中有些误解约MVC architectural pattern。如果你需要一些子弹为你的枪,刚拿到子弹本身,而且不需要另一把枪。所以我希望你理解加载控制器是一个坏主意。

此外,如果你想要一些变量的所有控制器都accessable,如拉夫·夏尔马 提到的,你可以使用Configure::write()将数据存储在应用程序的配置app/config/core.php .eg

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

然后你就可以Configure::read('somekey')进去$someval任何控制器。

0

今天早上我一直在为此工作。我实际上从数据库获取控制器名称,但我已将其更改为使用变量。

$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); 

老实说,我并没有理会搞清楚什么“通”是(我假设变量),但它抛出一个警告,没有它。 您还需要在您的$action中明确呼叫$this->render

+1

'通行证'是控制器中'passedArgs'变量的一半。如果你使用Dispatcher :: dispatch()而不是_invoke,它会为你设置这些,除了做其他可能有用的事情(但也可能是你的目的不必要的)。 – rintaun 2011-09-22 14:33:02