codeigniter,library或helper可以通过url访问吗?

Jin*_*Heo 1 php uri codeigniter

在Codeigniter中,有库和帮助器.我可以访问控制器及其子功能.例如.

login/getid
Run Code Online (Sandbox Code Playgroud)

有没有办法通过URL访问库或助手?

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

<img src="/login/get_captcha" />
Run Code Online (Sandbox Code Playgroud)

每次我想使用验证码时,我都要调用登录控制器.

所以,我认为应该有更好的方法来做到这一点.如果库或助手可以通过url访问,我可以帮助它.可以访问另一个控制器的视图而无需加载登录控制器.

Ali*_*guy 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)