简体   繁体   中英

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.

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';

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.

您需要在application/config/routes.php文件中添加一行

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM