Welcome Guest, Not a member yet? Register   Sign In
How to setup your codeigniter projects
#2

We usually start out creating a MY_Controller that will hold all the methods

that all our other controllers will need, these are placed in ./application/core.

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

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/15/2019
 * Time     : 12:58 PM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Base
 *
 * @project     ci3news
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2019 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class Base
 *
 * Saved at: ./application/core/Base.php
 */
class Base extends CI_Controller
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * Data variable for views.
     *
     * @var  array
     */
    
public $data = [];

    
/**
     * __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");
    }

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

  // End of Base Controller Class.

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

I then create two more core controllers one for the frontend and one for the backend.

Backend Controller: This controller extends the Base Controller.
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/15/2019
 * Time     : 1:01 PM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Backend
 *
 * @project     ci3news
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2019 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class Backend
 *
 * Saved at: ./application/core/Backend.php
 */
class Backend extends Base
{
    
/**
     * Class properties - public, private, protected and static.
     * -----------------------------------------------------------------------
     */

    /**
     * __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");
    }

    
/**
     * You would add your login and logout methods in this controller.
     */

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

  // End of Backend Controller Class.

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

Frontend Controller: This is the Frontend controller for all public methods.
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 1/15/2010
 * Time     : 1:05 PM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Frontend
 *
 * @project     ci3news
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2019 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

/**
 * Class Frontend
 * 
 * Saved at: ./application/core/Frontend.php
 */
class Frontend extends Base
{
    
/**
     * Class properties - public, private, protected and static.
     * -------------------------------------------------------------------
     */

    /**
     * __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");
    }

    
/**
     * All of your public methods will go here.
     */
    
    // -------------------------------------------------------------------

  // End of Frontend Controller Class.

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

And then to tie it all together we need an SPL Autoloader:
PHP Code:
// -----------------------------------------------------------------------

/**
 * Our SPL Autoloader.
 * -----------------------------------------------------------------------
 *
 * Add to the bottom of your ./application/config/config.php file.
 *
 */
spl_autoload_register(function ($class) {

    if (
strpos($class'CI_') !== 0)
    {
        if (
is_readable(APPPATH 'core/' $class '.php'))
        {
            require_once(
APPPATH 'core/' $class '.php');
        }
    }

}); 

Now all you would need to do is extend all your application controllers from either the
Frontend or Backend controllers depending on what you need.

Try to keep your application controllers small fast and lean, try to place most of your
business logic into your application models and libraries.

I try to place my controllers into sub folders to keep them ordered.
The same with my view files.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
RE: How to setup your codeigniter projects - by InsiteFX - 03-02-2019, 05:28 AM



Theme © iAndrew 2016 - Forum software by © MyBB