Are*_*ren 27
这是MVC系统不支持的行为.如果要执行另一个控制器的操作,只需将用户重定向到所需的页面(即使用该URL的控制器功能).
如果您需要常用功能,则应构建一个要在两个不同控制器中使用的库.
我只能假设你想要建立一个模块化的网站.(即在其他控制器方法中重用一个控制器方法的输出.)有一些CI的插件/扩展可以帮助您构建这样的.但是,最简单的方法是使用库来构建通用的"控件"(即加载模型,将视图渲染为字符串).然后你可以返回该字符串并将其传递给另一个控制器的视图.
您可以通过true
在视图调用结束时添加来加载到字符串中:
$string_view = $this->load->view('someview', array('data'=>'stuff'), true);
Run Code Online (Sandbox Code Playgroud)
小智 15
test.php
控制器文件:
Class Test {
function demo() {
echo "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
test1.php
控制器文件:
Class Test1 {
function demo2() {
require('test.php');
$test = new Test();
$test->demo();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
Very simple way in codeigniter to call a method of one controller to other controller
1. Controller A
class A extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_a()
{
}
}
2. Controller B
class B extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function custom_b()
{
require_once(APPPATH.'controllers/a.php'); //include controller
$aObj = new a(); //create object
$aObj->custom_a(); //call function
}
}
Run Code Online (Sandbox Code Playgroud)
您应该创建一个可以加载的帮助器或库,以便控制器可以共享功能.
V3:https://www.codeigniter.com/user_guide/general/creating_libraries.html? highlight/library
V2:https://www.codeigniter.com/userguide2/general/creating_libraries.html
V3:https://www.codeigniter.com/user_guide/general/helpers.html? highlight = help
V2:https://www.codeigniter.com/userguide2/general/helpers.html
小智 5
我同意这样做的方法是在通常的情况下重定向到新的控制器.
我遇到了一个用例,我需要向2种不同类型的用户显示相同的页面(后端用户预览前端用户的页面)所以在我看来我需要的是真正从后端控制器调用前端控制器.
我通过使前端方法静态并将其包装在另一个方法中解决了这个问题.希望能帮助到你!
//==========
// Frontend
//==========
function profile()
{
//Access check
//Get profile id
$id = get_user_id();
return self::_profile($id);
}
static function _profile($id)
{
$CI = &get_instance();
//Prepare page
//Load view
}
//==========
// Backend
//==========
function preview_profile($id)
{
$this->load->file('controllers/frontend.php', false);
Frontend::_profile($id);
}
Run Code Online (Sandbox Code Playgroud)
你可以这样做
$result= file_get_contents(site_url('[ADDRESS TO CONTROLLER FUNCTION]'));
Run Code Online (Sandbox Code Playgroud)
替换[ADDRESS TO CONTROLLER FUNCTION]
我们使用的方式site_url();
你需要echo output
在控制器功能而不是return
.
小智 5
您可以使用redirect()函数.像这样
class ControllerA extends CI_Controller{
public function MethodA(){
redirect("ControllerB/MethodB");
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
96335 次 |
最近记录: |