Custom URI routing by query string with CodeIgniter?

坚强是说给别人听的谎言 提交于 2019-12-11 01:54:20

问题


I am looking to make a custom route using the CodeIgniter framework. I am trying to make the URL like so:

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

So far I have tried adding the following to my routes.php config file:

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

but as you would guess, it doesn't work. I have also tried escaping the characters like this:

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

and that doesn't work either. I've also tried including the leading and trailing slashes .. that didn't work either. Anyone know by chance what could solve my issue?


回答1:


I highly recommend to use a SEF routing.

But if for any reason you're not eager to, you could check the query string inside the Accounts Controller, and then invoke the proper method, as follows:

Router:

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

Controller:

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

This allows you to invoke the methods by query string automatically.




回答2:


I am not sure, that its okay to use GET-params in routes.php config. Try such way:

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
}

But, as for me, it's bad way.

I recommend you just use another routing:

/accounts/Auth.dll/signin

And etc.



来源:https://stackoverflow.com/questions/21610199/custom-uri-routing-by-query-string-with-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!