1

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 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Perfect. This is what I'm looking for. But, I have a question, why is it it throw an error on the redirect part?. If I remove this, it's all working fine. I also autoloaded the uri but that line still throws an error.
You need to load the URL helper to use redirect() ellislab.com/codeigniter/user-guide/helpers/url_helper.html
Oh, they are loaded. What I mean is, if I add this code on the redirect, it will throw an error: $this-uri->segment(3)
Sorry, that was a typo on my part. It should be $this->uri->segment(3), edited to reflect.
i didn't notice that typo. lol.. by the way, thanks for helping. it helps a lot. Do you know any articles to boost my knowledge on codeigniter with an exception to their documentation?
0

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...

1 Comment

I've tried it. But it isn't effective as I need to write more codes later on my project and tends to confuse me later on. I'm new to MVC architecture and my previous sites are from native php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.