Welcome Guest, Not a member yet? Register   Sign In
Smart architecture for controller / views (body / header / footer) in CodeIgniter 3

(This post was last modified: 01-05-2020, 05:03 AM by imabot.)

I work with CodeIgniter for two years now. I'm the founder of Friconix and many other websites. All my website are based on CI 3. I love CI 3 because it is simple and fast

My last website keyller.io (currently under development) has a client-side JS engine and my architecture was no longer well suited.
I spend a lot of time on the Internet searching for the best practice for creating modular and well structured controllers/views. This morning, I had (what I believe being) a great idea for building the views in CI3I just want to share it with you for the following reasons:
  • it may help others;
  • it may be an inspiration for future CI version;
  • I think my solution is quite elegant.
A single view

I only display one view containing the page. Here is my main view:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="fr">
        <?= $header ?? '' ?>
        <div style="width:1200px">
                <?= $bodyContent ?? '' ?>

You'll easily understand it can be extended with JavaScript or any front-end add-on.


Here is my controller source code:

$data['header'] = (ENVIRONMENT === 'production') ? $this->load->view('templates/google-analytics', '', TRUE) : ''
            .$this->load->view('templates/javascript', '', TRUE)
            .$this->load->view('templates/css', '', TRUE);

$data['bodyContent'] = $this->load->view('templates/navbar', '', TRUE)
            .$this->load->view('templates/alert', '', TRUE)
            .$this->load->view('home/index', '', TRUE)
            .$this->load->view('home/footer', '', TRUE)
            .((!$logged) ? $this->load->view('templates/modal-login', '', TRUE) : '');

$this->load->view('templates/page', $data);

Look how beautiful and clear is the source code. You no longer have markup opened in one view and closed in another. Each view is now dedicated to one and only one thing.

Last, but not least, I love the way views are concatenated : method chaining pattern, so beautiful, I'm so proud Big Grin . It can potentially even being recursive, you can include views in views in views in views.

Question 1:

I would like to overload the method $this->load-view to add one parameter (display or not) to replace the following line :

.((!$logged) ? $this->load->view('templates/modal-login', '', TRUE) : '');

.$this->load->myView('templates/modal-login', '', TRUE, !$logged);

What is the best practice to overload this method ? Note that I never modified the CI core, I will not start today!

Question 2:

The following code is the same on many pages. I would like to create a function that can be called from any controller. 

$data['header'] = $this->load->view('templates/javascript', '', TRUE)
            .$this->load->view('templates/css', '', TRUE)
            .$this->load->view('templates/title-description', $dataHeader, TRUE)
            .$this->load->view('templates/favicon', '', TRUE);

In your opinion, where should I place this function? Helper?

Messages In This Thread
Smart architecture for controller / views (body / header / footer) in CodeIgniter 3 - by imabot - 01-05-2020, 03:34 AM

Theme © iAndrew 2016 - Forum software by © MyBB