2

I am working with CodeIgniter (V:2.2.6) and I have a simple class User with some basic methods like create, update, edit, delete and index. For the index function, I am using an argument $user (which is the second URI segment) in order to display some information regarding that user. So the default URL looks like:

/user/index/john

to display some information about the user 'john'.

Now, I want to remove the term 'index' from the URL, so that it looks like:

/user/john

For that purpose I have added the following rule in routes.php.

$route['user/(:any)'] = "user/index/$1";

It serves the purpose, but it prevents accessing other functions like /user/create and goes inside /user/index automatically. To solve this problem, I can not see any other way except manually adding routing rules like

$route['user/create'] = "user/create";

for each method of the User class, which is not cool at all. So, please can anyone suggest me a better way of routing under the current circumstances?

Here are my codes:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
    public function index($user = '') {
        echo "index!";
    }

    public function create() {
        echo 'create!';
    }
}

Note: I have gone through the CodeIgniter documentation for URI Routing and another similar question here, but could not figure out a promising solution. And please don't suggest for CodeIgniter version update.

2
  • Why is it "Not Cool" to add in your specific routes for create, update etc with the (:any) being last in the list? You wont go wearing out your keyboard by typing them in. Commented Nov 22, 2016 at 0:12
  • @TimBrownlaw well I kept the function list smaller for the sake of demonstration. Consider a real life scenario where the number of methods inside a class might be 10, 20 or 30. Commented Nov 22, 2016 at 3:31

1 Answer 1

4

I'm not sure, if this is the answer you like, but i think it should work. Just put this function in your user controller.

public function _remap($method)
{
    if (method_exists($this, $method))
    {
        $this->$method();
    }
    else
    {
        $this->index($method);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. Remapping Function Calls (codeigniter.com/userguide2/general/controllers.html#remapping) definitely sounds better than adding routing rules manually.
Alright, thx for accepting - but keep in mind you should prevent the possibility to create usernames like create, update, delete, insert and index.
CI3 has this issue, or my mamp config doesn't support something, I got the same issue with the index which was working by default before on other web apps. This snippet is a must have. Thanks @sintakonte!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.