Coherent theme and content in CodeIgniter

I am working on a basic website using CodeIgniter to better understand the structure and improve my limited web developer skills. I searched and read a lot of documentation, but did not find exactly what I was looking for (or at least I don’t think).

My site will have a consistent header, navigation, and footer. Essentially, the only content that will change through subnav is some text and / or images between the header and footer. This is pretty typical of a simple website, but I'm trying to figure out how to approach this in the context of views / controllers.

From my understanding in this scenario, you want to create a “theme” folder in the views, which contains the “main” view, which is loaded from each page. I also get the impression that with the ability to load multiple views in one controller, I really only need one "home" controller, which loads the "main" view along with any view associated with the navigator. So, for example, Nav: Home | About the project | contact. I press "O", and through the appropriate method of the home controller, I load the main + about views.

This is the approach I'm trying to take now, but I decided that when I try to do this, I get some feedback.

I'm not necessarily looking for a step-by-step guide, but even more so the accepted or most common approach. I really appreciate it, and I apologize in advance if it looked at me in the face.

Thanks -Jay

+4
source share
4 answers

So, you want to make sure that you understand the routes, to make sure everything displays the first segment of your URL, since you really only need one controller. This is located in the application / config folder in a file named routes.php

You need to configure it as follows:

$route['(:any)'] = "home/$1"; $route['default_controller'] = "home"; 

Thus, that matches any (in any of them) in the first segment and directs it to the house / $ 1 (controller / function) ... and then, if there is nothing in the first segment, it matches it to the house / (index function that implied). Take a look at the documentation for the route file so you can understand what you can do with it (you can run regular expressions, advanced routing, etc.), which is really important to know.

So in your home.php controller (controller / home.php or whatever you want to call) you can just have functions matching your url of your url

What is mentioned in other messages about the extension of the base controller, in this case is really not required. But it’s good to have a function that views the load for you so you don’t need to repeat $this->load->view(header) and $this->load->view(footer) , etc. for each function.

What I like to do is create an include folder with my header, footer, nav, etc., create a function that does just that ... with private access:

  private function viewloader($view, $data) { $this->load->view('includes/header', $data); $this->load->view('includes/nav', $data); $this->load->view($view, $data); $this->load->view('includes/footer', $data); } 

... or using the codeigniter built-in underscore before a function that makes it inaccessible via URL

  function _viewloader($view, $data) { $this->load->view('includes/header', $data); $this->load->view($view, $data); $this->load->view('includes/footer', $data); } 

Then your functions for each page will look something like this:

  function about() { /* put any application logic here */ $this->viewloader('about', $data); /* or $this->_viewloader('about', $data); if you went with CI style visibility */ } function contact() { /* put any application logic here */ $this->viewloader('contact', $data); /* or $this->_viewloader('contact', $data); if you went with CI style visibility */ } 

So now, as you can see, the viewloader function now loads the views of the header, nav, any kind of $ view and then the footer at the same time ... pretty nice.

You can also remember that you can load any views that you need in the view files themselves (nested views), they do not always need to be downloaded from the controller, although it’s good to keep them so that you don’t need to edit individual view files if you want make significant changes.

Here's what it might look like at the end:

 <?php if (! defined('BASEPATH')) exit('No direct script access'); class home extends CI_Controller { //php 5 constructor function __construct() { parent::__construct(); } function index() { $data['title'] = "Welcome To Our Site" $this->viewloader('home', $data); } function contact() { $data['title'] = "Contact Us" $this->viewloader('contact', $data); } function about() { $data['title'] = "About Us" $this->viewloader('about', $data); } private function viewloader($view, $data) { $this->load->view('includes/header', $data); $this->load->view('includes/nav', $data); $this->load->view($view, $data); $this->load->view('includes/footer', $data); } } 
0
source

You can try using the Template library. This is what I use for every codeigniter project that I do, and I like it.

Template

+1
source

I use my own mvc, but the concept is the same. I started creating a base controller class with the following methods.

 class base_controller { function assemble_display() { $this->display_view('head'); $this->display_main_content(); $this->display_view('foot'); } function display_main_content() { $this->display_view('home'); } } 

Then, in subsequent controllers that extend this base class, I overwrite the display_main_content () method.

 class search extends base_controller { function display_main_content() { $this->display_view('search'); } } 

That way I can call assemble_display () on any controller, and it will show the base head and leg, but with the correct main content.

0
source

CodeIgniter supports the inclusion of parts of your pages sequentially from the functions of your controller. I used to add my own base class in CI, which implements a set of custom view functions:

  • show($tab, $body, $data) - display the view using the header / body / sidebar / footer parts. The sidebar and body are pulled from the current $tab view folder.
  • _print($tab, $body, $data) - show(); option show(); for printing (sets CSS, removes unnecessary parts for printing). This can only be done using CSS, but I had clients that needed a special mojo server for printing. A.
  • widget($type, $id) - send a piece of the view (usually for js / Ajax)
  • api($type, $template) - send API data by type (JSON, XML, etc.).

This approach has worked well for a number of large projects, but there are other ways. CodeIgniter fork Kohana, for example, inverts the template and puts the logic of the page into templates. These templates perform the function of the base controller in a more natural way, so instead of several controller functions for different types of pages, different templates are used. The result is a set of view files, for example:

  • app/main-template.php
  • app/login-template.php
  • app/print-template.php
  • app/reports/wide-template.php
  • widgets/control-template.php
  • api/soap-template.php
  • etc.

The Kohana approach can be applied to CodeIgniter, and it is more common in other environments (for example, Rails, Cake, Zend, etc.).

0
source

Source: https://habr.com/ru/post/1336688/


All Articles