2011-09-28 31 views
1

在Codeigniter中,有库和帮助程序。 我可以访问控制器及其子功能。例如 。codeigniter,库或帮手可以通过url访问吗?

login/getid 

有什么办法通过url访问库或帮手吗?

更新: 我在登录控制器中创建了一个验证码库。 我想在许多其他控制器的视图中使用它。 在视图文件,图形验证码应该是这样的,

<img src="/login/get_captcha" /> 

每次我想要使用验证码,我要打电话登录控制器。

所以,我认为应该有更好的方法来做到这一点。 如果库或助手可以通过url访问,我可以将其作为帮助。 可以在不加载登录控制器的情况下访问另一个控制器的视图。

回答

5

您可以创建一个包装控制器访问专门的功能和使用你的路线,利用该URL的

例子:yoursite.com/helper/geo/citiesNearZip/90210

class helperController extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper($this->uri->segment(1)); // geo helper in this example 

     if($this->uri->segment(2)) 
     { 
      $helper_method = $this->uri->segment(2); 
     } 
     else 
     { 
      show_404(); 
      return false; 
     } 

     // check if helper has function named after segment 2, function citiesNearZip($zip) in this example... 
     if(function_exists($helper_method) 
     { 
      // Execute function with provided uri params, xss filter, secure, etc... 
      // You would also want to grab all the remaining uri params and pass them as 
      // arguments to your helper function 
      $helper_method(); 
     } 
    } 
} 
+0

正确的,我可以空包装控制器,并能访问。我知道这个。谢谢。 –

+0

也许这是最好的方法... –

+0

这是一个非常糟糕的主意。你不能错误地处理你的url调用 - 如果“geo”是一个不存在的帮助器,代码将会简单地爆炸。这是一个“整洁的黑客”,但编码错误 – jmadsen

2

没有。这根本不是框架设计工作的方式。

如果您认为您必须直接访问帮助程序/库,那么您可能做错了什么。

能解释你想要做什么吗?一定会有更好的办法。

相关问题