1

I'm working with code igniter and for some reason, the url http://mysite.com/account/100 gives me a 404 error but http://mysite.com/account actualy works. Here's what my controller account.php looks like.

   class account extends Controller {


    function account()
    {
      parent::Controller();
    }

    function index()
    {
     echo 'hello';
      echo $this->uri->segment(2);
    }
    }

Any idea what's wrong?

5
  • It may be that it is trying to call 100 as a method instead of an argument so try account/index/100 and see if it changes. Commented Mar 29, 2010 at 22:24
  • ah you are right. Is it possible to have the 100 not treated as a function? For example, if function not found, then treat it as a url variable? Commented Mar 29, 2010 at 22:39
  • There doesn't appear to be, at least not without editing CodeIgniter itself. Commented Mar 29, 2010 at 22:49
  • I take that back, you can use routes to do it for instance. $route['account/(:num)'] = "account/index/$1" I believe that would work. See the URI Routing user guide for more info. codeigniter.com/user_guide/general/routing.html Commented Mar 29, 2010 at 22:55
  • great thanks, can you add that routing tip to your answer, then i'll check mark it Commented Mar 29, 2010 at 22:57

3 Answers 3

4

I just tested a simple account class like you have and it is trying to call 100 as a method of account, instead your URL after index.php should be account/index/100 and it works fine.

This can be solved using routing for example.

$route['account/(:num)'] = "accounts/index/$1";

is what you are looking for. You can look over the URI Routing user guide for more info.

CodeIgniter requires that your controller name be capitalized, and the filename lowercase so try changing your controller to.

class Account extends Controller { }

and your filename account.php

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

1 Comment

sorry, that was just a typing mistake in my question. I actually ahve the bracket in my controller and it is named account.php
0

Add this route and you are good

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

Comments

0

This is not working because when you request http://example.com/account, it looks index() method. But when you request http://example.com/account/100, is looking for 100() method. Which is not present. You may tweak the code like this.

class account extends Controller {
 public function account()
 {
   parent::Controller();
 }

 public function index()
 {
  echo 'hello';
 }

 public function id(){
  echo $this->uri->segment(2);
 }
 public function alternative($id){
  echo $id;
 }
}

you'll call url like this: http://example.com/account/id/100

or you can do like this http://example.com/account/alternative/100

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.