1
votes

I have a question which regards to controllers. Let's get it started:

  • I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.

So for this example:

I have 3 controllers: admin.php , company.php, usermanagement.php

What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement

I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.

How would I achieve this?

by the way here's a snippet of my code:

admin.php - Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

    function __construct(){
        parent::__construct();

        session_start();
    }

    public function index() {
          $this->load->view('view_admin');
       }
}

Company - Same as the admin

  <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Company extends CI_Controller {

        function __construct(){
            parent::__construct();

            session_start();
        }

        public function index() {
              $this->load->view('view_company');
           }
    }

Thanks, James

EDIT: I have tried adding functions on the admin.php like:

function company() {} function usermanagement() {}

but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.

2

2 Answers

2
votes

If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:

class Company extends CI_Controller {
    public function __construct() {
        parent::__construct();
        if ( $this->uri->segment(1) != "admin" ) {
            redirect('admin/company/'.$this->uri->segment(3));
        }
    }

...

Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.

0
votes

You can add more functions in your 'admin' controller, so, default page is:

 public function index() {
              $this->load->view('view_admin');
           }

'Subpage' is:

 public function company() {
              $this->load->view('view_company');
           }

etc, etc...