如何在codeigniter中从另一个控制器加载控制器


How to load a controller from another controller in codeigniter?

我想从另一个控制器的函数中加载一个控制器因为我集成到我的项目中的库我不想将它加载到控制器中因为我想保持它的干净和相关。

我尝试使用模块,但我仍然需要将控制器放在url中,如

http://example.com/maincontroller/function
http://example.com/othercontroller/function

我有一个默认的控制器,所以我可以加载http://example.com/function,所以我怎么能从main的函数访问控制器,所以我不需要把控制器放在url中。

如果我可以从主控制器函数加载控制器函数,我仍然愿意使用HMVC。

可以(对于版本2)

在你的控制器中像这样加载

 $this->load->library('../controllers/whathever');

,并调用以下方法:

$this->whathever->functioname();

你不能从CI中的控制器加载控制器-除非你使用HMVC或其他东西。

你应该考虑一下你的架构。如果你需要从另一个控制器调用控制器方法,那么你可能应该将代码抽象到一个助手或库中,并从两个控制器调用它。

再次阅读您的问题后,我意识到您的最终目标不一定是HMVC,而是URI操作。纠正我,如果我错了,但它似乎是你试图完成url与第一部分是方法名称,并把控制器名称一起。

如果是这样的话,你可以通过创造性地设计你的路由来得到一个更简洁的解决方案。

对于一个非常基本的例子,假设您有两个控制器,controller1controller2Controller1method_1 -方法,controller2method_2方法。

你可以这样设置路由:

$route['method_1'] = "controller1/method_1";
$route['method_2'] = "controller2/method_2";

然后,您可以使用URL(如http://site.com/method_1)调用方法1,使用http://site.com/method_2调用方法2。

虽然,这是一个硬编码的,非常基本的例子,但它可以让你到达你需要的地方,如果你需要做的只是从URL中删除控制器。


你也可以重新映射你的控制器

来自文档:"如果你的控制器包含一个名为_remap()的函数,无论你的URI包含什么,它都会被调用。":

public function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}

不能直接从另一个控制器调用控制器方法

我的解决方案是使用继承并从库控制器

扩展控制器。
class Controller1 extends CI_Controller {
    public function index() {
        // some codes here
    }
    public function methodA(){
        // code here
    }
}
控制器中的

我们称之为Mycontoller它会扩展Controller1

include_once (dirname(__FILE__) . "/controller1.php");
class Mycontroller extends Controller1 {
    public function __construct() {
        parent::__construct();
    }
    public function methodB(){
        // codes....
    }
}

你可以从mycontroller

调用methodAhttp://example.com/mycontroller/methodA

http://example.com/mycontroller/methodB

这个解决方案为我工作

我有一个类似的问题。我想有两个控制器:

home .php -面向公众的主页

home.php -用户登录后的主界面

,我想让他们都从'mydomain.com'读取

我可以通过在我的路由配置中将'hompepage'设置为默认控制器并在home .php中添加一个remap函数来实现这一点

function _remap()
{
  if(user_is_logged_in())
  {
    require_once(APPPATH.'controllers/home.php'); 
    $oHome =  new Home();
    $oHome->index();
  }
  else
  {
    $this->index();
  }
}

我有会话文件没有发现错误,而尝试了各种方法,最终实现了这样的。使函数为静态(我想在另一个控制器中调用),并调用为

require_once('Welcome.php');
Welcome::hello();

虽然上面的方法可能有效,但这里有一个非常好的方法。

用一个MY控制器扩展核心控制器,然后将这个MY控制器扩展到所有其他控制器。例如,您可以这样写:

class MY_Controller extends CI_Controller {
    public function is_logged()
    {
        //Your code here
    }
public function logout()
    {
        //Your code here
    }
}

那么你的其他控制器可以扩展如下:

class Another_Controller extends MY_Controller {
    public function show_home()
    {
         if (!$this->is_logged()) {
           return false;
         }
    }
public function logout()
    {
        $this->logout();
    }
}

我来到这里是因为我需要在Twig中创建一个{{ render() }}函数,以模拟Symfony2的行为。从视图中渲染控制器非常酷,可以显示独立的小部件或ajax可重载的东西。

即使你不是Twig用户,你仍然可以使用这个帮助器,并在你的视图中使用它来渲染控制器,使用<?php echo twig_render('welcome/index', $param1, $param2, $_); ?>。这将回显控制器输出的所有内容。

在这里:

助手/twig_helper.php

<?php
if (!function_exists('twig_render'))
{
    function twig_render()
    {
        $args = func_get_args();
        $route = array_shift($args);
        $controller = APPPATH . 'controllers/' . substr($route, 0, strrpos($route, '/'));
        $explode = explode('/', $route);
        if (count($explode) < 2)
        {
            show_error("twig_render: A twig route is made from format: path/to/controller/action.");
        }
        if (!is_file($controller . '.php'))
        {
            show_error("twig_render: Controller not found: {$controller}");
        }
        if (!is_readable($controller . '.php'))
        {
            show_error("twig_render: Controller not readable: {$controller}");
        }
        require_once($controller . '.php');
        $class = ucfirst(reset(array_slice($explode, count($explode) - 2, 1)));
        if (!class_exists($class))
        {
            show_error("twig_render: Controller file exists, but class not found inside: {$class}");
        }
        $object = new $class();
        if (!($object instanceof CI_Controller))
        {
            show_error("twig_render: Class {$class} is not an instance of CI_Controller");
        }
        $method = $explode[count($explode) - 1];
        if (!method_exists($object, $method))
        {
            show_error("twig_render: Controller method not found: {$method}");
        }
        if (!is_callable(array($object, $method)))
        {
            show_error("twig_render: Controller method not visible: {$method}");
        }
        call_user_func_array(array($object, $method), $args);
        $ci = &get_instance();
        return $ci->output->get_output();
    }
}

特定于Twig用户(根据您的Twig实现调整此代码):

库/Twig.php

$this->_twig_env->addFunction('render', new Twig_Function_Function('twig_render'));
使用

{{ render('welcome/index', param1, param2, ...) }}

使用我下面创建的代码创建一个帮助器,并将其命名为controller_helper.php。

config下的autoload.php文件中自动加载您的帮助器。

从你的方法调用controller('name')来加载控制器。

注意name是控制器的文件名

此方法将'_controller'附加到控制器'name'。要在控制器中调用方法,只需在如上所述加载控制器后运行$this->name_controller->method();

<?php
if(!function_exists('controller'))
{
    function controller($name)
    {
        $filename = realpath(__dir__ . '/../controllers/'.$name.'.php');
        if(file_exists($filename))
        {
            require_once $filename;
            $class = ucfirst($name);
            if(class_exists($class))
            {
                $ci =& get_instance();
                if(!isset($ci->{$name.'_controller'}))
                {
                    $ci->{$name.'_controller'} = new $class();
                }
            }
        }
    }
}
?>

您可以通过多种方式从一个控制器访问另一个控制器。

class Test1 extends CI_controller
{
    function testfunction(){
        return 1;
    }
}

然后创建另一个类,并在其中包含first class,并使用您的类扩展它。

include 'Test1.php';
class Test extends Test1
{
    function myfunction(){
        $this->test();
        echo 1;
    }
}

你可以从控制器调用Model,所以把你的函数放在一个模型中,然后从控制器调用它。对我有用。(codeignitor 3)