将一个控制器加载到 cakephp 中的另一个控制器中

发布于 2024-09-02 19:38:19 字数 309 浏览 5 评论 0原文

我正在开发一个 Web 应用程序,使用多个页面,每个页面都有自己的控制器。

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

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

我通过添加来做到这一点 应用程序::导入('控制器', '部分');

$sections=新的sectionsController; $sections->constructClasses();

到控制器,但这似乎不起作用..

也许你们有一些想法?

提前致谢!

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, that 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗化ㄋ丶相逢 2024-09-09 19:38:19

我认为您对 MVC 存在一些误解架构模式。如果你的枪需要一些子弹,只需获取子弹本身即可,不需要再获取另一把枪。所以我希望你明白加载控制器确实是一个坏主意。

另外,如果您希望所有控制器都可以访问一些变量,如 Gaurav Sharma
提到,你可以使用 Configure::write() 将数据存储在应用程序的配置 app/config/core.php.eg 中,

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

然后你可以得到 $someval 通过在任何控制器中的 Configure::read('somekey') 实现。

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.

锦上情书 2024-09-09 19:38:19

您可以使用以下任一方法来访问 cakePHP 应用程序中任意位置的变量。
1.) 使用 配置类 或 < br>
2.) 对这些变量使用会话

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

天暗了我发光 2024-09-09 19:38:19

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

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

老实说,我没有费心去弄清楚“pass”的用途(我假设是变量),但如果没有它,它会发出警告。
您还需要在 $action 中显式调用 $this->render

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文