Custom URI routing by query string with CodeIgnite

2019-07-19 17:13发布

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?

2条回答
来,给爷笑一个
2楼-- · 2019-07-19 17:49

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.

查看更多
萌系小妹纸
3楼-- · 2019-07-19 18:11

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.

查看更多
登录 后发表回答