-1

I'm having some troubles with codeigniter and the MVC model. In my webpage I have a main controller with functions that handles the usual navigation menu with different views home page,about,support... but I have a login view.

  1. I'm not sure if this separation is correct or I should create a controller for each view.
  2. How do I call the login controller, I'm using base_url('main/myView/'); to call the functions inside the main controller, but if I call the login controller from the login view base_url('login/foo'); it does not work.

I'm new to codeigniter and I read their tutorial but I still not sure when should I create a new controller.

Thanks

tereško
  • 57,247
  • 24
  • 95
  • 149
Juanjo
  • 69
  • 1
  • 7

2 Answers2

0

There are many auth libraries for Codeigniter. I'm sure use the library is better than you write it.

Codeigniter 3.x Authentication Library?

https://github.com/benedmunds/CodeIgniter-Ion-Auth

https://github.com/jenssegers/codeigniter-authentication-library

Community
  • 1
  • 1
Mostafa Soufi
  • 445
  • 6
  • 16
-1

You don't need to create a multi-controller for each view. you can do it all with one controller.

If you need another controller you can create a new controller and assign it in routes.php. For now, I'm creating 2 controller. Remember controller name start with the capital letter

// pages controller
class Pages extends CI_Controller { // `application/controller/Pages.php`
    public function __construct(){
        parent::__construct();     
    }
    public function index(){
        // default_controller
    }
    public function about(){
        // pages/about
    }
    public function support(){
        // pages/support
    }
}

// admin controller
class Admin extends CI_Controller { // `application/controller/Admin.php`
    public function __construct(){
        parent::__construct();     
    }
    public function login(){
        // admin/login
    }
    public function logout(){
        // admin/logout
    }
}

Assign url to the controller in application/config/routes.php

$route['default_controller'] = 'pages';   // call lowercase letter
$route['about'] = 'pages/about';
$route['support'] = 'pages/support';

$route['login'] = 'admin/login'; // call lowercase letter
$route['logout'] = 'admin/logout';

If you call base_url('login'), admin controller login function will work

If you call base_url('about'), pages controller about function will work

Anfath Hifans
  • 1,560
  • 1
  • 9
  • 18