codeigniter页脚钩子或扩展CI

系统教程 行业动态 更新时间:2024-06-14 17:02:18
codeigniter页脚钩子或扩展CI_Controller(codeigniter footer hook or extending CI_Controller)

我正在使用Codeigniter构建我的页面,我遇到了一个小问题,我使用模板浏览我的页面。 这些模板包含标题,main_content和页脚。 直到现在还可以,但后来我意识到,我想将一些数据放入我需要从数据库加载的页脚。 类似于搜索次数最多的10个术语。

我正在寻找一种合适的方法来做这件事,我开始决定两种选择。 我可以将我的CI_Controller扩展到application/core/的MY_Controller,或者我可以创建一个钩子 ,可以准备要传递给每个控制器的数据。

还有其他可能更好的选择吗?

I am using Codeigniter to build my page and I ran into a small problem, I am using templates to navigate through my page. These templates contain a header, main_content and a footer. Till now it was ok but then I realised, I want to put some data into the footer that I need to load from the database. Something like the 10 most searched terms.

I was searching for an appropriate way of doing this and I came into deciding between 2 options. I can extend my CI_Controller into a MY_Controller in application/core/ or I can create a hook that can prepare the data to be passed to every controller.

Is there any other, possibly better, option to stick to?

最满意答案

我在MY_Controller中有一个名为_load_view 。 确保在控制器中扩展父控制器。 我所具有的功能类似于以下内容......这在application > core > MY_Controller.php 。

public function _load_view($page,$data = null) { $data['view'] = $page; // used for a css document based on the view you're on $this->load->view('header/header',$data); // header contains all the meta tags/header js/css $this->load->view('navigation',$data); // simply just contains the navigation (only did this for ease of updating the navigation) $this->load->view($page,$data); // this loads your content page that would be the template that you insert all your data from your controller into $this->load->view('footer/footer',$data); // finally load the footer, containing js, and all that fun stuff. }

注意:页脚视图文件和页眉文件将放在views文件夹中的页眉和页脚目录中。

在您的默认控制器中,确保您拥有以下范围的内容......

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // makes it so that there is no way that someone can access this file class Home extends MY_Controller { public function index() { $data = array(); $this->_load_view('view_file',$data); // references the function you created in the parent class. } }

就数据库查询而言,我建议在_load_view()函数中执行此操作。 这可确保数据在任何页面上都可用,并且可以防止您不得不一遍又一遍地重写代码。 我看到它的方式是帮助程序是您需要全面访问的功能。 在模型中进行查询,然后在控制器中加载该功能。

这可确保您在每个页面上加载页眉,导航和页脚,并且它是一致的。 传入$data数组是可选的,因为如果没有传递数据它将默认为null,并且它将在所有模板中可用。 订单也很重要。 希望这可以帮助! 如果您有任何问题,请告诉我,我会相应更新我的答案!

I have a function within a MY_Controller that's called _load_view. Make sure you extend the parent controller in your your controllers. The function that I have looks something like the following... This being in application > core > MY_Controller.php.

public function _load_view($page,$data = null) { $data['view'] = $page; // used for a css document based on the view you're on $this->load->view('header/header',$data); // header contains all the meta tags/header js/css $this->load->view('navigation',$data); // simply just contains the navigation (only did this for ease of updating the navigation) $this->load->view($page,$data); // this loads your content page that would be the template that you insert all your data from your controller into $this->load->view('footer/footer',$data); // finally load the footer, containing js, and all that fun stuff. }

Note: The footer view files and header files will be placed in a header and footer directory within the views folder.

In your default controller make sure that you have something to the following extent...

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // makes it so that there is no way that someone can access this file class Home extends MY_Controller { public function index() { $data = array(); $this->_load_view('view_file',$data); // references the function you created in the parent class. } }

In terms of the database query I'd suggest doing that in the _load_view() function. This ensures that the data is available on ever page and would prevent you from having to rewrite the code over and over again. The way I see it is helpers are for functions that you need access to across the board. Do the querying within a model and then load the function in the controller.

This ensures that you have the header, navigation, and footer loaded on every page, and it's consistent. Passing in the $data array is optional as it'll default to null if the data isn't passed through, and it will be available in all the templates. The order does matter as well. Hope this helps! Let me know if you have any questions and I'll update my answer accordingly!

更多推荐

本文发布于:2023-04-21 18:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bb271bb3760063ff97dc901e9bedf49a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:钩子   codeigniter   CI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!