2

I'm trying to pass parameters to a control in codeigniter, but I'm getting 404 page not found error, I don't get it, I did what the guide says: http://codeigniter.com/user_guide/general/controllers.html#passinguri

When I remove the params in the index function and just access the controller everything works fine, but I can't pass a value to it...

Here is the code the way I'm trying to send a param:

http://mysite/123

<?php
class Main extends Controller {

    function index($username) {

        echo $username;

    }

}
?>

How can I get more info regarding this error from codeigniter?

Thank you.

2 Answers 2

10

With that URL, CodeIgniter can't understand if you want to pass 123 to the index method or if you're requesting the 123 method with no parameters. You have to explicitly name the default method if you need to pass it some parameters.

http://mysite/index/123
Sign up to request clarification or add additional context in comments.

4 Comments

Is there anyway to bypass it, and make it understand that I'm sending it to index only? Thank you.
If you only have the index method in that controller, you can set up rewrite via codeigniter's routing; if you have other methods how can you tell if a segment is a method name or an argument for index?
I'm only passing parameters like this when the controller has only the index function.
BTW, http://mysite/index/123 doesn't work either... It needs the name of the controller in the path: http://mysite/main/index/123 Why???
5

Option 1 - Rempap the function call in your controller

If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules. http://codeigniter.com/user_guide/general/controllers.html#remapping

Option 2 - Use a custom route.

http://codeigniter.com/user_guide/general/routing.html

1 Comment

OK, but how do I deal with the fact that codeigniter asks me to specify the controller name as well, like so: mysite/main/index/123 main is defined as my default controller in the directory, why do I need to specify it?

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.