简体   繁体   English

如何在cakephp中调用控制器后立即调用函数

[英]How to call a function as soon as controller is called in cakephp

I am creating a simple website in cakephp. 我在cakephp中创建一个简单的网站。

I have kept header and footer in "element" but my problem is that menu of header is coming from database and it will not be available until I call a function. 我将页眉和页脚保留在“元素”中,但是我的问题是页眉菜单来自数据库,并且在调用函数之前将不可用。

I want as soon as controller is called function of menu should be called by own. 我想控制器一经调用就应该自己调用菜单功能。

In your AppController add this: 在您的AppController中添加以下内容:

function beforeFilter(){
    $this->set('menu', $this->YourModel->findById('Id of your menu data row in database'));
}

Then, in the view, the menu will be accessible by calling echo $menu['yourModel']['menu']; 然后,在视图中,可以通过调用echo $menu['yourModel']['menu'];来访问echo $menu['yourModel']['menu'];

TLDR: TLDR:

Instead of calling the function every time the entire controller (or all controllers) is loaded, it's better practice to just call the function when the element itself is loaded. 与其在每次加载整个控制器(或所有控制器)时都调用该函数,不如在加载element本身时才调用该函数。 This allows you much more flexibility moving forward. 这使您可以更加灵活地前进。 Eg, maybe the log-in screen doesn't need the menu - or maybe you eventually add an admin tool that has a different menu...etc etc. 例如,也许登录屏幕不需要菜单-也许您最终添加了具有其他菜单的管理工具...等等。

CakePHP makes this VERY easy using requestAction() - read more here: http://book.cakephp.org/2.0/en/views.html#passing-variables-into-an-element CakePHP使用requestAction()使此操作非常容易-在此处了解更多信息: http : //book.cakephp.org/2.0/en/views.html#passing-variables-into-an-element

Side Note: It's also ideal (MVC pattern and many other reasons) to keep ALL queries in a model, instead of calling them directly from a controller. 旁注:将所有查询保留在模型中,而不是直接从控制器调用它们也是理想的(MVC模式和许多其他原因)。

Example Code: 示例代码:

/**
 * MenusController (or any other controller you want)
 */
public function get_menu_main() {
    $this->set('menu', $this->Menu->getMenuMain());
}
public function get_menu_footer() {
    $this->set('menu', $this->Menu->getMenuFooter());
}

/**
 * Menu model
 */
public function getMenuMain() {
    return $this->findById('12345');
}
public function getMenuFooter() {
    return $this->findById('67891');
}

Then, in your element, just use a requestAction to retrieve the data you need for that specific element: 然后,在您的元素中,只需使用requestAction即可检索该特定元素所需的数据:

/**
* MainMenu element
*/
$menu = $this->requestAction('menus/get_menu_main/');

/**
* FooterMenu element
*/
$menu = $this->requestAction('menus/get_menu_footer/');

Side note: to be a little more proper, you can use $this->Html->url() instead of hard-coding the URLs to the actions. 旁注:为更适当一点,您可以使用$this->Html->url()而不是将URL硬编码为操作。

Side note: Some of this code might seem like adding extra code when you don't "need" it, but by breaking things up into their correct MVC spot, your code will be much cleaner, more flexible/upgradable...etc etc etc. Getting into a habit of doing things like this will make your life MUCH easier when things start to get even slightly more complicated. 旁注:当您不需要时,其中一些代码可能看起来像添加了额外的代码,但是通过将它们分解成正确的MVC位置,您的代码将变得更加简洁,更加灵活/可升级...等等当事情开始变得更加复杂时,养成这样的习惯会使您的生活变得更加轻松。 (And in all reality, the code above adds a few lines, but as far as complexity is concerned, I think it's easier to understand than having a query in the AppController that loads variables for an element(s). /endrant (实际上,上面的代码增加了几行,但是就复杂性而言,我认为比在AppController中查询为元素加载变量更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM