Search code examples
phpcodeigniterdirectory-structureaccess-control

codeigniter handle code when application controller directory get accessed


My directory settings

-- application 
   -- controllers 
      -- admin 
         -- manage 

When I accessed 'localhost/admin', it redirect to 'localhost/'.
Question is :
- Where should I handle this ? I want to do something when someone accessed 'localhost/admin' , not just redirect them to 'localhost/'.

Thx


Solution

  • So it's a three part answer.

    You need a controller named (in this example) example.php. You need a view in your view folder called example_view.php. And you need to edit your config/routes file.

    First, create a controller in your admin folder. In this case I'll call it example.php. It should look like this:

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Example extends CI_Controller {
    
    /*
    | -------------------------------------------------------------------
    | CLASS CONSTRUCTOR FUNCTION
    | -------------------------------------------------------------------
    */    
        public function __construct() {
            parent::__construct();
    
        } 
    /*
    | -------------------------------------------------------------------
    | VIEW FOR INDEX LAUNCHING PAGE
    | -------------------------------------------------------------------
    */   
    public function index() {
            $this->load->view('example_view');
    }
    
    
    }//end controller class
    
    /* End of file example.php */
    /* Location: ./application/controllers/admin/example.php */
    

    Second create a view called example_view.php and put it in your view folder.

    Third open config/routes.php and add this line: $route['admin'] = "admin/example";

    This last part points anything that is called localhost/admin to the view indicated in your example.php index() function.