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

How do you guys arrange/structure your projects?

How do you guys structure your controllers? 

how do you separate your functions into specific controllers?

For example: login,logout,register functions should go inside accounts controller.

Let's say i want to make an online shopping web app using codeigniter.

Therefore i will have browse_items function and create_item.

The former doesnt need a logged in account but the latter needs an authenticated account.

Do i then make 2 versions of item controller and put them in separate subfolder inside my controller folder named private/public?
Reply
#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
#3

@InsiteFX What is the advantage of your approach? It looks good, I just didn't understand the needs of Base.php controller. Are you doing a HMVC approach?
Reply
#4

No I'am not using HMVC, The Base controller will hold all the methods that are needed
in the Frontend and Backend controllers. The Backend controller if for Admin methods etc;
The Frontend is for the public methods for the users etc;

Once you get these setup and working the way you want you just leave them alone.

If you want you can just combine all the classes into just a ./application/core/MY_Controller.php
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(03-02-2019, 11:42 AM)InsiteFX Wrote: No I'am not using HMVC, The Base controller will hold all the methods that are needed
in the Frontend and Backend controllers. The Backend controller if for Admin methods etc;
The Frontend is for the public methods for the users etc;

Once you get these setup and working the way you want you just leave them alone.

If you want you can just combine all the classes into just a ./application/core/MY_Controller.php

So, you created three controllers: base, backend and frontend, right? But you said


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

Sorry if I didn't understand, but why do you need the Base controller then? So, for example how do you dedice each methods go into Base and each ones go into Backend controller?
Reply
#6

Because the Base controller holds all of the methods that would be used in the other two controllers.

Like $data for the views in both controllers and say a renderView method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB