Code Igniter controller - can't use uri->segment with function index()? Code Igniter controller - can't use uri->segment with function index()? codeigniter codeigniter

Code Igniter controller - can't use uri->segment with function index()?


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 guidefor 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


Add this route and you are good

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


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