【问题标题】:Custom URI routing by query string with CodeIgniter?使用 CodeIgniter 通过查询字符串自定义 URI 路由?
【发布时间】:2014-02-06 17:44:47
【问题描述】:

我正在寻找使用 CodeIgniter 框架的自定义路由。我正在尝试使 URL 像这样:

http://localhost/accounts/Auth.dll?signin

到目前为止,我已经尝试将以下内容添加到我的 routes.php 配置文件中:

$route['accounts/Auth.dll?signin'] = "accounts/signin";

但正如您猜想的那样,它不起作用。我也尝试过像这样转义字符:

$route['accounts/Auth\.dll\?signin'] = "accounts/signin";

这也不起作用。我也尝试过包括前导和尾随斜杠..这也不起作用。任何人偶然知道什么可以解决我的问题?

【问题讨论】:

    标签: php codeigniter codeigniter-2 codeigniter-url codeigniter-routing


    【解决方案1】:

    我强烈建议使用 SEF 路由。

    但是,如果出于某种原因您不想这样做,您可以检查 Accounts 控制器内的 查询字符串,然后 invoke 正确的 方法,如下:

    路由器:

    $route['accounts/Auth.dll'] = "accounts";
    

    控制器:

    class Accounts extends CI_Controller
    {
        public function __construct()
        {
            # Call the CI_Controller constructor
            parent::__construct();
    
            # Fetch the query string
            if ($method = $this->input->server('QUERY_STRING', TRUE)) {
                # Check whether the method exists
                if (method_exists($this, $method)) {
                    # Invoke the method
                    call_user_func(array($this, $method));
                }
            }
        }
    
        protected function signin()
        {
            # Your logic here
        }
    }
    

    这允许您通过查询字符串自动调用方法。

    【讨论】:

    • 感谢推荐SEF url的建议,但这只是针对小团队,没有人会使用搜索引擎找到这个。
    • 你节省了我的时间!我一直在尝试通过 Apache mod_rewrite 规则来实现这一点。
    【解决方案2】:

    我不确定,是否可以在 routes.php 配置中使用 GET-params。 试试这样:

    routes.php

    $route['accounts/Auth.dll'] = "accounts/index";
    

    accounts.php

    public function index() {
         if ($this->input->get('signin') != false) {
              $this->signin();
         }
    }
    
    private function signin() {
        // some code
    }
    

    但是,对我来说,这是不好的方式。

    我建议你只使用另一个路由:

    /accounts/Auth.dll/signin
    

    等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多