2012-12-25 44 views
0

我是Codeigniter的新手。 我有3个自动加载库在config.phpCodeigniter中的自动加载库

但在我的一个控制器中,我不想加载库。这可能吗?

+1

这是做有点棘手的事情,becouse自动加载的是全球性的功能。看看这篇文章[http://stackoverflow.com/questions/8096630/not-to-load-an-autoload-library-in-codeigniter](http://stackoverflow.com/questions/8096630/not- load-an-autoload-library-in-codeigniter) – Sasha

+0

@Sasha Thanks mate !! ..-) – raavan

回答

2

扩展库中的CI_Controller。

事情是这样的:

class MyLibrary extends CI_Controller { 
    var $ci; 

    function __construct() { 

     $this->ci = &get_instance(); 
     $route = $this->ci->router->fetch_class(); 

     if($route == strtolower('YourController')) return false; 
    } 
} 
0

您可以从自动加载文件中删除库。那么他们将不会在框架中激活。 如果你想使用它们,你可以在构造函数中调用它们,如果你想让它们在一个类中。如果你想在方法中使用它们,你可以在方法中加载它们。

6

如果你需要在整个应用程序中的任何库,你可以加载它在配置文件中,它会被自动加载。但是,如果只需要特定控制器中的库,则可以将其加载到需要它的控制器中。

Class test Extends CI_Controller{ 

    function index() 
    { 
     $this->load->library('mylibrary'); 
     $this->mylibrary->somemethod();  
    } 

} 

或者,如果你需要库通过控制器,你可以在构造函数中加载它。

Class test Extends CI_Controller{ 

    function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('mylibrary'); 
    } 

    function index(){ 
     $this->mylibrary->somemethod();  
    } 
    function test(){ 
     $this->mylibrary->someothermethod();  
    } 

} 
+0

在构造函数中加载模型真的有用吗?从来没有尝试过这种方式......有趣。 – esp

+0

是的,它的工作方式与图书馆一样 –