2

我正在寻找使用CodeIgniter框架进行自定义路由。我试图使URL像这样:通过CodeIgniter查询字符串自定义URI路由?

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

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

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

但你猜,这不工作。我也尝试逃避这样的字符:

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

而且这也不起作用。我也尝试过,包括前导斜线和尾部斜线......也没有效果。任何人都偶然知道什么可以解决我的问题?

回答

3

我强烈建议使用SEF路由。

但是,如果由于任何原因,你是不是渴望,你可以检查查询字符串Accounts控制器里面,然后调用适当方法,如下所示:

路由器:

$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 
    } 
} 

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

+0

感谢您推荐SEF网址的建议,但这只是一个小团队,没有人会使用搜索引擎来找到这个。 –

+0

@AndrewButler当然:) –

+0

你已经保存了我的时间!我一直试图通过Apache mod_rewrite规则来实现这一点。 – kta

0

我不确定,它可以在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