3

I have a controller name 'abc'

Now, I define a index function in it.

Now as i go to www.example.com/abc/ or www.example.com/abc/index i can see my page appearing.

Now when i pass argument in that index function i would have to call it as:

 www.example.com/abc/index/argument

So, How can i call my argument as

 www.example.com/abc/argument

without treating this 'argument' as public function ?

2
  • 3
    _remap() will do the trick this question has been answer many times on stackoverflow see here please also with my comment below the answer stackoverflow.com/questions/18686389/… Commented Sep 11, 2013 at 10:38
  • Routing, _remap(), call_user_func_array are all good methods. Honestly though, just use "$argument = $this->uri->segment(1);". Clean, organized, and simple. stackoverflow.com/questions/16424480/… Commented Nov 8, 2013 at 4:40

5 Answers 5

7

you can add routing for it in your routes.php file, like:

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

doing this will route your url www.example.com/abc/argument to www.example.com/abc/index/argument

Sign up to request clarification or add additional context in comments.

3 Comments

Might cause unexpected behavior if he tried to call /abc/index/argument and get /abc/index/index/argument.
@sudhir Iam asking somethin else
@Rahul Maybe you should clarify what you are asking then, because this is looks like a good way to answer your problem.
4
+25

Are you looking for remapping ?

public function _remap($method, $params = array())
{
    if (method_exists($this, $method))
    {
       return call_user_func_array(array($this, $method), $params);
    } 
    else 
    {
       return $this->index($method);
    }
    show_404();
}

Comments

1

The only way to use it is to have a new controller.

In your case it should be name as argument that could make it work as you wish it to.

Comments

1

Unless you want to overwrite CI_Exceptions/ function show_404 which will provide unstable solution , you could try using another function d inside your class abc :

    function d (){
        $argument= $this -> uri -> segment(3);
        if ($argument) {
            //do something
        }
    }

and then you call it with this: www.example.com/abc/d/argument

Comments

0

You can create an init function in your controller class and then check if there any function named your first argument by function_exist() follow http://php.net/manual/en/function.function-exists.php, and then call your index function by $this->index() and pass the argument to the index function by $this->index($arg1).

Comments

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.