Jim Johnson
Jim Johnson

Reputation: 2609

What is required to call a controller in Codeigniter?

I'm creating a simple blog with Codeigniter. But I'm having trouble calling another controller besides the default controller.

The following URL takes me to the default controller as specified in my config/routes.php.

blog/index.php

According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed:

blog/index.php/blog_login

Here is my controller class, named blog_login.php:

class Blog_login extends CI_Controller {

    public function index()
    {
        echo 'It works!';
    }
}

But doing this throws a 404 error, which makes me feel that I'm missing something. Is there something else that I am supposed to configure before trying to access a different controller?

Upvotes: 0

Views: 257

Answers (2)

Rega
Rega

Reputation: 880

When u call:

blog/index.php/blog_login

you're really calling a method called "blog_login" in your "blog" controller. If you want to call another controller, it must have the following structure:

controller_name/controller_method

So, if you wanna call your blog_login controller just call it like this:

blog_login/

Note: Sometimes it's necessary to add the base_url() to your URL in order to make CI understand correctly the URL.

Upvotes: 0

stef
stef

Reputation: 27769

http://codeigniter.com/user_guide/general/routing.html Read this properly, it couldn't be clearer.

According to the documentation, simply appending the name of another controller saved in controllers/ is all that is needed

This is not true. If you want to call another controller 'Blog_login', you simply put the name of the controller as the first segment of the url:

domain.com/index.php/blog_login

This will never work:

blog/index.php/blog_login

Index.php (unless you remove it via .htaccess) always follows right after your domain.com

Finally, you don't need to specify routes unless you're doing something non standard. So

domain.com/index.php/blog_login - calls the index() function in your Blog_login controller domain.com/index.php/blog - calls the index() function in your blog controller domain.com/index.php/blog/search - calls the search() function in your blog controller.

None of the above examples need an entry in routes.php

Upvotes: 0

Related Questions