【问题标题】:codeigniter, library or helper can access through url?codeigniter, library 或 helper 可以通过 url 访问吗?
【发布时间】:2011-11-26 19:58:24
【问题描述】:

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

login/getid

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

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

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

每次我想使用验证码时,我都必须调用登录控制器。

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

【问题讨论】:

    标签: php codeigniter uri


    【解决方案1】:

    您可以创建一个包装控制器来专门访问这些功能并使用您的路由来利用所述 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();
            }
        }
    }
    

    【讨论】:

    • 对,我可以清空包装控制器,并且可以访问它。我知道这个。谢谢。
    • 这是一个非常糟糕的主意。这样做你根本无法错误处理你的 url 调用 - 如果“geo”是一个不存在的助手,代码将简单地爆炸。这是一个“整洁的黑客”,但编码不好
    • 一个简单的检查 file_exists(APPPATH . '/helpers/' . $this-&gt;uri-&gt;segment(1) . '.php') 是否足够我想。 404 如果它丢失。所有代码都可以针对用例进行调整。我不认为你在这里的偏执狂个人认为反对票是合理的,但感谢你的意见。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多