Php 如何在codeigniter中组合不同的页面?
假设我把我的页面分成4个部分。一个是横幅,一个是登录,一个是主,另一个是页脚。横幅和页脚是静态的,不会更改。而登录是基于不同的用户权限或登录状态,最后主要是基于很多事情来做。所以,我有元素的4个部分:Php 如何在codeigniter中组合不同的页面?,php,codeigniter,Php,Codeigniter,假设我把我的页面分成4个部分。一个是横幅,一个是登录,一个是主,另一个是页脚。横幅和页脚是静态的,不会更改。而登录是基于不同的用户权限或登录状态,最后主要是基于很多事情来做。所以,我有元素的4个部分: http://localhost:8888/my_app/index.php/static_page/banner http://localhost:8888/my_app/index.php/static_page/footer http://localhost:8888/my_app/in
http://localhost:8888/my_app/index.php/static_page/banner
http://localhost:8888/my_app/index.php/static_page/footer
http://localhost:8888/my_app/index.php/user/login_fragment
http://localhost:8888/my_app/index.php/system/main_fragment
但是用户只看到一个页面,但是它将所有的工作人员组合在一起……因此,与其使用iframe将他们放在一起,我如何让这些页面分离逻辑,但可以组合在一个视图中?多谢各位 只需多次调用加载->查看 在控制器方法中:
function index()
{
$this->load->view("banner",array(/** some parameters if you want **/));
$this->load->view("main",array(/** some parameters if you want **/));
$this->load->view("footer"); // this one doesn't need parameters maybe
}
请参阅文档页面
注意:在视图文件本身(例如inside banner.php)中,可以再次调用load view以插入嵌套视图
注2:如果不想立即输出,也可以通过添加true参数作为第三个参数,将load->view的结果返回到变量中:
$login_box = $this->load->view("login",array(),true);
// $login_box contains the view html.
echo $login_box; // or pass it as a parameter to another view if you want
只需多次调用load->view 在控制器方法中:
function index()
{
$this->load->view("banner",array(/** some parameters if you want **/));
$this->load->view("main",array(/** some parameters if you want **/));
$this->load->view("footer"); // this one doesn't need parameters maybe
}
请参阅文档页面
注意:在视图文件本身(例如inside banner.php)中,可以再次调用load view以插入嵌套视图
注2:如果不想立即输出,也可以通过添加true参数作为第三个参数,将load->view的结果返回到变量中:
$login_box = $this->load->view("login",array(),true);
// $login_box contains the view html.
echo $login_box; // or pass it as a parameter to another view if you want
我想分享我所做的事情。虽然不是一个原创的想法(不记得我从哪里得到的),希望这能帮助你。我有一个“includes”[application/views/includes]文件夹,其中包含以下文件: footer.php、header.php、navigation.php和template.php 现在template.php中的代码如下所示:
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
关于用法,我给你举个例子:
$data = array (
'page_title' => 'Users Listing',
'title' => 'Users Listing',
'main_content' => 'users/showlist',
);
$this->load->view('includes/template', $data));
$data数组中的元素将在视图[application/views/users/showlist.php]中以$page\u title、$title等形式提供。您还可以发送数组或HTML内容。我想分享我所做的工作。虽然不是一个原创的想法(不记得我从哪里得到的),希望这能帮助你。我有一个“includes”[application/views/includes]文件夹,其中包含以下文件: footer.php、header.php、navigation.php和template.php 现在template.php中的代码如下所示:
$this->load->view('includes/header');
$this->load->view($main_content);
$this->load->view('includes/footer');
关于用法,我给你举个例子:
$data = array (
'page_title' => 'Users Listing',
'title' => 'Users Listing',
'main_content' => 'users/showlist',
);
$this->load->view('includes/template', $data));
$data数组中的元素将在视图[application/views/users/showlist.php]中以$page\u title、$title等形式提供。您还可以发送数组或HTML内容。首先
$this->load->view('filename');
可以在同一控制器功能中使用任意次数,并且它们将以正确的顺序显示
您所做的是在控制器中分离必须在同一控制器中使用的部分。我想你有这样的档案
/application/controllers/static_page.php
/application/controllers/user.php
/application/controllers/system.php
而且,如果不大规模修改CodeIgniter,您就不能从另一个控制器调用控制器,这正是阻止您的原因。如果必须对许多函数使用相同的处理代码,则必须创建一个可以在任何地方使用的解决方案,或者创建一个更优雅的解决方案,即在/libraries/MY_controller.php中使用公共控制器函数
MY_Controller.php将始终在调用控制器之前被调用
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// you can do any kind of processing here
// in example if you have some view counter
// this place is always called
}
function a_generic_function($rawdata)
{
// do some processing here
return $data;
}
}
现在,处理页眉+页脚的最聪明的方法是创建一个包含页眉和页脚的“布局”文件
下面是layout.php视图的一个示例:
<!doctype html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<header>This page has been made by <?php echo $user_name; ?>/header>
<div id="main" role="main">
<?php echo $main_content_view; ?>
</div>
<footer>Write your footer here</footer>
</div>
</body>
</html>
你还能用这个做什么?您可以使用$this->viewdata,如下所示:
$this->viewdata["title"] = 'Function title';
$this->viewdata["user_name"] = $user_name;
因此,您可以像任何其他视图一样编辑您的布局。首先
$this->load->view('filename');
可以在同一控制器功能中使用任意次数,并且它们将以正确的顺序显示
您所做的是在控制器中分离必须在同一控制器中使用的部分。我想你有这样的档案
/application/controllers/static_page.php
/application/controllers/user.php
/application/controllers/system.php
而且,如果不大规模修改CodeIgniter,您就不能从另一个控制器调用控制器,这正是阻止您的原因。如果必须对许多函数使用相同的处理代码,则必须创建一个可以在任何地方使用的解决方案,或者创建一个更优雅的解决方案,即在/libraries/MY_controller.php中使用公共控制器函数
MY_Controller.php将始终在调用控制器之前被调用
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
// you can do any kind of processing here
// in example if you have some view counter
// this place is always called
}
function a_generic_function($rawdata)
{
// do some processing here
return $data;
}
}
现在,处理页眉+页脚的最聪明的方法是创建一个包含页眉和页脚的“布局”文件
下面是layout.php视图的一个示例:
<!doctype html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<header>This page has been made by <?php echo $user_name; ?>/header>
<div id="main" role="main">
<?php echo $main_content_view; ?>
</div>
<footer>Write your footer here</footer>
</div>
</body>
</html>
你还能用这个做什么?您可以使用$this->viewdata,如下所示:
$this->viewdata["title"] = 'Function title';
$this->viewdata["user_name"] = $user_name;
因此,您可以像任何其他视图一样编辑您的布局。Phil Sturgeon为Codeigniter编写了一个流行的模板库,它将以比目前其他答案中描述的更智能的方式完成您想要的任务。您可以在此处阅读其文档: 文档可能需要一段时间才能理解,因为它缺少示例
您可以在此处下载模板库:Phil Sturgeon为Codeigniter编写了一个流行的模板库,它将以比目前其他答案中描述的更智能的方式完成您想要的任务。您可以在此处阅读其文档: 文档可能需要一段时间才能理解,因为它缺少示例
您可以在这里下载模板库:但是我有一些主要的逻辑…在控制器中从数据库获取信息,并进行一些检查,如果我可以直接下载,它不会加载所有的数据…如果我直接调用视图。@ted。不是100%确定你的意思,但你应该从数据库中加载信息,并在你的控制器方法中进行处理,然后你可以使用第二个参数(检查视图文档的链接)将一些数据传递给你的视图,但我有一些主要逻辑…在控制器中,从数据库获取信息,并进行一些检查,如果我直接调用视图,它不会加载所有数据。@ted。不是100%确定你的意思,但是你应该从数据库中加载信息并进行处理