一个视图两个控制器,CodeIgniter?

发布于 2024-11-08 11:18:31 字数 179 浏览 0 评论 0原文

大家好,我是 CodeIgniter 的新手,需要一些帮助。我有一个控制器,用于格式化帖子的内容区域。问题是我还需要创建一个包含动态组的侧边栏和一个包含最近帖子的右栏。这并不难,我遇到的问题是我想要每个页面上的侧边栏和右栏,并且我不想重新编码相同的位以获取每个控制器中的数据。

在不复制/粘贴的情况下执行此操作的最佳方法是什么?

Hey guys, I am new to CodeIgniter and need some help. I have a controller that formats the content area of a post. The problem is that I also need to create a sidebar that contains dynamic groups, and a right column that contains recent posts. This isn't hard, the problem I'm running into is that I want the sidebar, and right column on every page, and I don't want to recode the same bits to get the data in every controller.

What would be the best way to do this without copy/paste?

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

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

发布评论

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

评论(6

依 靠 2024-11-15 11:18:31

有很多方法可以做到这一点。

1)模板化:这是我在大多数情况下的偏好(因为我的模板很复杂),我使用以下内容将视图渲染为变量:

$content = $this->load->view('myview', $page_data, true);

然后将其加载到模板解析器中(仅供参考,您也可以将其加载到另一个视图中),例如这样:

$this->load->library('parser');
$data = array(
        'page_title' => 'My Page Title',
        'page_content' => $content,
        'side_bar' => side_bar(), // function which generates your side bar
        'right_col' => right_col() // function which generates your right column
        );
$this->parser->parse('my_template', $data);

那么你的模板就像:

<html>
<head>
<title>{page_title}</title>
</head>
<body>
    <div>{side_bar}</div>
    <div>{page_content}</div>
    <div>{right_col}</div>
</body>
</html>

2)在你的视图中加载另一个视图:(假设你的菜单是一个视图而不是控制器)像这样:

<?php $this->load->view('menu_view'); ?> 

3)PHP包含:正是你将如何在普通PHP中做到这一点(只需包含一个指向控制器的 url它返回一个菜单),像这样:

<?php include("/common/sidebar"); ?>

Codeigniter 将渲染该页面,然后包含它。

4) AJAX..如果“模板”内容中的内容不太重要,例如横幅、建议的相关项目列表等,我会使用它。

There are a lot of ways to do this.

1) Templating: This is my preference for most cases (because my templates are complex), I render my view into a variable using something like:

$content = $this->load->view('myview', $page_data, true);

Then I load it into the template parser (fyi you could load it into another view too) like this:

$this->load->library('parser');
$data = array(
        'page_title' => 'My Page Title',
        'page_content' => $content,
        'side_bar' => side_bar(), // function which generates your side bar
        'right_col' => right_col() // function which generates your right column
        );
$this->parser->parse('my_template', $data);

Then your template is like:

<html>
<head>
<title>{page_title}</title>
</head>
<body>
    <div>{side_bar}</div>
    <div>{page_content}</div>
    <div>{right_col}</div>
</body>
</html>

2) Load another view in your view: (assumes you menu is a view not a controller) Something like this:

<?php $this->load->view('menu_view'); ?> 

3) PHP Includes: exactly how you would do it in plain PHP (just include a url which points to a controller which returns a menu), Something like this:

<?php include("/common/sidebar"); ?>

Codeigniter will render that page and then include it.

4) AJAX.. i use this if the content in the "template" content is less important, like banners, suggested related item lists and such.

音盲 2024-11-15 11:18:31

使用PHP生成静态HTML页面,如side_bar.html...
然后您可以将其包含在其他页面上。

Use PHP to generate a static HTML page, such as side_bar.html...
Then you can include it on other pages.

简美 2024-11-15 11:18:31

您可以查看 HMVC。它特别适合您所说的“小部件”类型区域。

本质上,您要做的就是创建两个完整的 MVC 结构 - 一个用于侧边栏和右栏,包括一个控制器、一个模型(如果需要)和一个部分视图。然后,您可以直接从主视图调用该控制器,将所需的内容拉入页面。

要实际从视图中调用它,只需将以下内容放置在标记中您希望侧边栏出现的任何位置:

索引不是必需的,但我将其放在那里是为了演示您可以使用 modules::run() 调用不同的方法。您还可以向 modules::run() 传递无限数量的参数。

You could look into HMVC. It's especially suited for "widget"-type areas like you are talking about.

Essentially what you will do is create two full MVC structures - one for your sidebar and right column, including a controller, a model(if required), and a partial view. Then, you can call this controller directly from the main view to pull the required content in to the page.

To actually call it from within a view, just place the following in the markup wherever you want the sidebar to appear:

<?php echo modules::run('module/sidebar/index'); ?>

The index isn't required, but I put it there to demonstrate that you can call different methods using modules::run(). You can also pass an unlimited number of parameters to modules::run().

妥活 2024-11-15 11:18:31

在代码点火器中,$this->load->view 有一个可选的第三个参数,它允许您以字符串形式返回渲染的视图,该视图又可用于分配。您可以做的是创建一个主模板,其中包含所有公共部分,作为一个非常简单的示例:

<html>
<head>
</head>
<body>
<?php echo $sidebar; ?>
<?php echo $content; ?>
<?php echo $right_column; ?>
</body>
</html>

然后您可以在控制器中创建一个私有函数来填充公共部分的动态内容,并将它们与您的内容和主模板:

private function BuildTemplate($view, $data) {
   // Generate sidebar content
   $sidebar_data['...'] = 'blah blah';
   $master_data['sidebar'] = $this->load->view('sidebar', $sidebar_data, true);

   // Generate right column data
   $right_data['...'] = 'blah blah';
   $master_data['right_column'] = $this->load->view('right_column', $right_data, true);

   // Now load your content
   $master_data['content'] = $this->load->view($view, $data, true);

   // Merge it into the master template and return it
   return $this->load->view('master' $master_data, true);
}

然后在适当的控制器方法中:

public function index() {
   $data['...'] = 'blah';
   echo $this->BuildTemplate('index', $data);
}

这将为您将所有内容整合在一起。如果您想添加页面特定标题或脚本等内容,您可以选择向 BuildTemplate 添加额外参数。

In code igniter, there is an optional third parameter to $this->load->view that lets you return a rendered view as a string, which can in turn be used for assignment. What you can do is create a master template, that has all the common parts, as a very simplified example:

<html>
<head>
</head>
<body>
<?php echo $sidebar; ?>
<?php echo $content; ?>
<?php echo $right_column; ?>
</body>
</html>

Then you can create a private function in your controller to populate the dynamic content of your common parts, and combine them with your content and master template:

private function BuildTemplate($view, $data) {
   // Generate sidebar content
   $sidebar_data['...'] = 'blah blah';
   $master_data['sidebar'] = $this->load->view('sidebar', $sidebar_data, true);

   // Generate right column data
   $right_data['...'] = 'blah blah';
   $master_data['right_column'] = $this->load->view('right_column', $right_data, true);

   // Now load your content
   $master_data['content'] = $this->load->view($view, $data, true);

   // Merge it into the master template and return it
   return $this->load->view('master' $master_data, true);
}

Then in your appropriate controller method:

public function index() {
   $data['...'] = 'blah';
   echo $this->BuildTemplate('index', $data);
}

Which will pull everything together for you. You can optionally add extra arguments to BuildTemplate if you want to add things like page specific titles or scripts.

停顿的约定 2024-11-15 11:18:31

我不确定您的问题是在视图中,还是在该视图(的公共部分)中显示的(动态)数据中。
如果是后者(似乎暗示着“我不想重新编码相同的位以获取每个控制器中的数据”),那么您有多种选择。例如。

  1. 将获取“通用”数据的逻辑放入控制器外部的某个函数中(作为助手或某个模型内部),并从控制器中调用它。

  2. 让您的控制器继承您自己的自定义控制器,以实现该数据收集功能。

  3. 将两个控制器重构为一个控制器,为每个场景提供不同的功能。

I'm not sure if your problem is in the view, or in the (dynamic) data to be shown in the (common parts of) that view.
If it's the later (as seems to suggest the phrase 'I don't want to recode the same bits to get the data in every controller'), then you have several options. For example.

  1. Put the logic to get the 'common' data in some function outside the controller, as a helper or inside some model, and call it from your controllers.

  2. Make your controllers inherit your own custom controller, that implements that data gathering function.

  3. Refactor your two controllers into a single controller, with different functions for each scenario.

辞旧 2024-11-15 11:18:31

1-使用以下代码在库文件夹中创建一个自定义库类

if (!defined('BASEPATH')) exit('No direct script access allowed');

class LoadView{

   function __construct(){
    $this->CI =& get_instance();
   }
   function load_view($page){
       $this->CI->load->view('header');
       $this->CI->load->view('sidebar');
       $this->CI->load->view($page);
       $this->CI->load->view('footer');
   }

}

2-现在将此库加载到您的控制器中,如下所示

$this->load->library('loadview');

3-现在调用库方法并只需插入您的页面名称,您不必包含标题、侧边栏和页脚一次又一次,因为它们将动态地包含在您的库中。

$this->loadview->load_view('about.php');

1-Create a custom library class in library folder with the below code

if (!defined('BASEPATH')) exit('No direct script access allowed');

class LoadView{

   function __construct(){
    $this->CI =& get_instance();
   }
   function load_view($page){
       $this->CI->load->view('header');
       $this->CI->load->view('sidebar');
       $this->CI->load->view($page);
       $this->CI->load->view('footer');
   }

}

2-Now load this library in your controller like this

$this->load->library('loadview');

3-Now call the library method and simply insert your page name and you don't have to include header,sidebar and footer again and again as they will be dynamically included by your library.

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