CodeIgniter Forums
several controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: several controller (/showthread.php?tid=74046)



several controller - keiralittle - 07-12-2019

Hi , sorry for english , in every controller i have :

Code:
Code:
       $this->load->view('templates/header_user',$data);
       $this->load->view('templates/menu_user',$data);
       $this->load->view('controller/view', $data);
       $this->load->view('templates/footer_user',$data);


Because menu_user are used in several controller  and i want send a value  to menu , how can i do this ?


RE: several controller - InsiteFX - 07-12-2019

Create a MY_Controller

Comments on how to use in the header section of the class.

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class Base
 * 
 * File must be saved as MY_Controller.php in
 * ./application/core/MY_Controller.php
 * 
 * Place all your code that is used in all controllers
 * in this controller and then extend all controllers with Base
 * 
 * class Your_Controller extends Base
 * 
 */
class Base extends CI_Controller
{
 
   /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


 
   // -------------------------------------------------------------------

 
   /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       log_message('debug'"Base Controller Class Initialized");
 
   }

 
   /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
 
   public function index()
 
   {

 
   }

 
   // -------------------------------------------------------------------

  // End of Base Controller Class.

/**
 * Class Backend
 */
class Backend extends Base
{
 
   /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


 
   // -------------------------------------------------------------------

 
   /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       log_message('debug'"Backend Controller Class Initialized");
 
   }

 
   /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
 
   public function index()
 
   {

 
   }

 
   // -------------------------------------------------------------------

  // End of Backend Controller Class.

/**
 * Class Frontend
 */
class Frontend extends Base
{
 
   /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


 
   // -------------------------------------------------------------------

 
   /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     */
 
   public function __construct()
 
   {
 
       parent::__construct();

 
       log_message('debug'"Frontend Controller Class Initialized");
 
   }

 
   /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
 
   public function index()
 
   {

 
   }

 
   // -------------------------------------------------------------------

  // End of Frontend Controller Class.

// End of MY_Controller Controller Class.


/**
 * -----------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * -----------------------------------------------------------------------
 */ 

Make sure that you extend all your controllers from Base

Or use can extend the controller using Frontend or Backend


RE: several controller - vincent78 - 07-12-2019

Hello,
I've created a library that manages the templates of a website: CodeIgniter Layout Library
It saves you from writing the 4 loads in every controller.