Zach Reed
Zach Reed

Reputation: 683

CodeIgniter "default_controller" functions without controller name in URL

Forgive me if this is a "duh" question or if you need more information to answer, I am new to CodeIgniter and still haven't figured out a few things with best practices and such...

In routes.php I have $route['default_controller'] = "home"; so my default_controller is obviously "home".

Inside my home.php controller I have:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
    function __construct() {
        // blah
    }
    function index(){
        // blah
    }
    function login(){
        // blah
    }
}

Which works fine and everything, there is no problems with that. The only thing I can't figure out is if I want to access the login function, I currently have to go to www.blah.com/home/login. How can I change it so it just goes to www.blah.com/login, without creating a new controller (I would like to keep some of these one time base urls all in my default controller)? Is that even possible or do I just have to create a new controller?

If I just have to create a new one, is there a best practices for how many controllers you have, etc.

Upvotes: 3

Views: 3697

Answers (3)

user1190992
user1190992

Reputation: 669

You need add a line to your application/config/routes.php file

$route['login'] = "home/login";

Upvotes: 0

Mudshark
Mudshark

Reputation: 3253

The way I have it set up is to have the index function act as the gatekeeper: if not logged in, show a login form view, if logged in, redirect to your (protected) home page. The login function is only accessed via post, when the user submits his login/pwd, and performs the login logic.

Upvotes: 0

Waqar Alamgir
Waqar Alamgir

Reputation: 9968

Documentation says: This route will tell the Router what URI segments to use if those provided in the URL cannot be matched to a valid route.

So use $route['login'] = 'home/login';

Upvotes: 4

Related Questions