2010-05-19 106 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

相關問題