Search code examples
phpcodeignitercodeigniter-routingcodeigniter-3

CodeIgniter - Routing urls with GET Variables


I checked the similar questions out there, but those doesnt seem to help.

I have an email verification like, that is to be routed in CodeIgniter to the right function with data passed to the function for further processing.

Sample URL :

http://mysite.dev/verify/?id=emailaddress@gmail.com&hash=562828a975740ac6820e40f7f61b4407

Current Route :

$route['verify/(:any)'] = 'formcontroller/verification/$1';

Function :

public function verification($slug)
    {
        parse_str(parse_url($slug, PHP_URL_QUERY), $fileds);
        var_dump($fields);
    }

The problem is, I get a 404 when I try the url listed above. I get Message: Undefined variable: fields when I try something like http://amazon.dev/verify/asdasdasd

Can someone point me in the right direction?

ADD : If there is no ? in the url, it works. But for the standard of a get query url should be, I would like like to know how to solve this


Solution

  • The problem actually was, the query started after a /.

    Actually the url should be in this model, ? immediately after the directory name.

    http://mysite.dev/verify/?id=emailaddress@gmail.com&hash=562828a975740ac6820e40f7f61b4407
    

    Route should be

    $route['verify'] = 'formcontroller/verification';
    

    And the variables should be handled using get method of CodeIgniter.

    $this->input->get('variablename');

    Also, the url should be urlencoded just incase if there are any characters which are not exclusively allowed by the config file.