1

I am really stuck with this problem

in CodeIgniter i have controller named user.php

and it has two function getUser() and save()

when I try to call http://localhost/CodeIginter/index.php/user/save

it always call getUser function why ?

class user extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    function getUser()
    {
        $this->load->model('usermodel');
        $data['query'] = $this->usermodel->get_last_ten_entries();
        $this->load->view('users',$data);
    }

    function save()
    {
        $this->load->model('usermodel');
        $this->usermodel->insert_entry();
        $this->load->view('users');
    }
}

my .htaccess file contain

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

and i am also unable to load another controller that is helloworld.php

2
  • 2
    Maybe you have wrong routes at your codeigntiter project. Can you please copy your routes.php file content? Commented Aug 24, 2013 at 16:28
  • $route['default_controller'] = "user"; $route['404_override'] = ''; Commented Aug 24, 2013 at 16:38

5 Answers 5

2

step-1: change this .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /your_folder_name/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

step-2: application/config/config.php

$config['base_url'] = 'full_path';
$config['index_page'] = '';

step-3: application/config/routes.php

$route['default_controller'] = "user";

step-4: user class add this function

public function index(){
}
Sign up to request clarification or add additional context in comments.

Comments

1

There could be 2 problems:

1. Missing route :

Goto application/routes.php

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

2.You need to make function public if you want to call it from outside in url.

So,

class user extends CI_Controller
{
function __construct()
{
    parent::__construct();
}

public function getUser()
{
    $this->load->model('usermodel');
    $data['query'] = $this->usermodel->get_last_ten_entries();
    $this->load->view('users',$data);
}

public function save()
{
    $this->load->model('usermodel');
    $this->usermodel->insert_entry();
    $this->load->view('users');
}
}

Hope your problem is solved now:)

1 Comment

You don't need to write a route for every controller as it beats the purpose of CI. And methods are considered public by default.
1

Controller names must be capitalized. So, User instead of user.

class User extends CI_Controller
class Helloworld extends CI_Controller
...

The file names remain in lowercase though.

Other than that, everything seems fine.

Comments

0

Goto application/config/routes.php

Comments

-3

It seems you are missing a route. Add the following line to your routes.php file

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

And now it should be working as expected

1 Comment

You don't need to write a route for every controller as it beats the purpose of CI.

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.