views:

31

answers:

1

Hello all

I am trying to route a URL using codeigniter URL routing. I want to redirect a url like

  • /users/edit?email to userController/editemail
  • /users/edit?password to userController/editpassword

I tried using the following line in routes.php in config folder

$route["users/edit?(email|password)"] = "userController/edit$1";

This displays page not found. I am guessing that ? is being treated as a regular expression character. I tried escaping it but that didn't work either.

I don't want to set the config setting uri_protocol to PATH_INFO or QUERY_STRING, since this is just a pretty URL I want to setup, not pass anything to the action.

Can somebody help me out over here?

Regards

A: 

You should escape the ? like this, it should work. (not tested)

$route["users/edit\?(email|password)"] = "userController/edit$1";

Later edit:

This works as intended:

$route["users/edit(email|password)?"] = "userController/edit$1";

The userController looks like this

<?php

class UserController extends Controller {

    function edit()
    {
        echo "general edit";
    }

    function editemail()
    {
        echo "edit email!";
    }

    function editpassword()
    {
        echo "edit password";
    }
}

Router works like this:

  • if you go to http://sitename/index.php/users/editemail you see editemail() action.
  • if you go to http://sitename/index.php/users/editpassword you see editpassword() action.
  • if you go to http://sitename/index.php/users/edit you see the edit() action (the question mark makes optional the email/password field and you can do some other things in edit() action
Bogdan Constantinescu
@Bogdan - had already tried it and failed. Wonder if this has got something to do with permitted URL characters, it doesn't have ? anywhere in it. But its not showing the "URI has disallowed characters" message. It shows "Page not found".
ShiVik
I made a test and now works. Check later edit! :)
Bogdan Constantinescu
@Bogdan - I didn't get that. What you wrote in later edit, isn't that supposed to work for urls like `users/editemail`, `users/editpassword` and `users/edit`? Where is the question mark in URL? I'm having problem in `users/edit?password`.
ShiVik
@Bogdan - the solution you are suggesting requires me to change my URL specification. I was curious whether it can be done in CI without changing it.
ShiVik
You have to find another way since `?` doesn't work. I looked in `~/libraries/Router.php` and `_validate_request()` function does not validates URL rewrites with `?` inside, and also leads to a bogus `$segments` array. You should try using `/` or any other method, like the one I described above
Bogdan Constantinescu
@Bogdan - alright. Thanks for that insight.
ShiVik