• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
logo

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Write For US
  • News
  • About Us
  • Snippets
You are here: Home / PHP / CodeIgniter – Basic Concepts

CodeIgniter – Basic Concepts

Sep 7, 2017 by Editorial Staff 1 Comment

Codeigniter is a small footprint MVC framework that helps programmers to create robust web applications. It is considered as one of the smallest frameworks when coming to the size and learning curve.

What are the basic concepts in codeigniter ?

Controllers

Controllers are the entry point in any web application. So controller does same duty in Codeigniter as well. It is the URL that we can access via web browser.

Creating a controller

First go to application/controllers folder and create file called Movies.php. You will find two other files already there, index.html and Welcome.php, they are shipped with Codeigniter.

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

class Movies extends CI_Controller {

	
	public function index()
	{
		echo "hello wolrd";
	}
}

This Movies class will extend a class called CI_Controller, a Codeigniter core library.

Calling a controller

http://yourdomaincom/index.php/movies/index

This will print the text “hello world”. You may have noticed word movies after index.php, it is the controller name and word index is the method name. So we will call the URL like http://yourdomaincom/index.php/controller/method_name

In the next example you will see an extra method, that will be responsible for view a particular movie.

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

class Movies extends CI_Controller {
	
	public function index()
	{
		echo "hello wolrd";
	}
	public function view()
	{
		echo "The Movie One";
	}
}

You can see the hello world example in the browser

Codeigniter Hello word controller

 

Views

Views are the real theme of the website, so it includes all the relevant html,css,js assets.  We can call the view through the controllers. Let’s create a view in the application/views folder, this is the default Codeigniter view folder. Below I have added a sample code of view in Codeigniter.

<!DOCTYPE html> 
<html lang = "en"> 

   <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter View</title> 
      <link href="css/styes.css" rel="stylesheet">
   </head> 	
   <body> 
      Here comes your html
      <div>Alert</div>
   </body>
	
</html>

 

We can call the view through a controller as like below.

* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
    $this->load->view('index');
}

Where index is the view name, which codeigniter will find in the folder application/view/index.php, if it can’t find the file it will throw a 404 error.

If you want more cleaner approach, suppose all the movie related in one view and TV series related view in different folder you can use the below method. so this will be finding respective directories application/views/movies/index.php and  application/views/tv/index.php

$this->load->view('movies/index');
$this->load->view('tv/index');

Models

Models are designed to connect with database directly. For example, you have movie database, Models will be responsible for insert,update,list, and delete movie articles.

Model classes are stored in the application/models folder. See below for a sample class in Codeigniter.

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

class Movie_model extends CI_Model {

        public $title;
        public $content;
        public $date;

        public function get_last_ten_entries()
        {
                $query = $this->db->get('entries', 10);
                return $query->result();
        }       

}

So you can see Movie_model is the class name, and the file must be saved as Movie_model.php in the application/models directory.

Loading a model

Model can be called in controller. Following code can be used to load any model.

$this->load->model('model_name');

You can call a method from the model in the controller like this

$this->movie_model->get_last_ten_entries()

 


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.

View all posts by Editorial Staff

Filed Under: PHP Tagged With: Codeigniter Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar




Quick Links

  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Base64 Decode Online
  • Base64 Encode Online

Subscribe

* indicates required

Share

   

Hot topics

  • A Simple YouTube Video Downloader Script in PHP 272 views
  • Replace “\n” with new line characters, using Notepad++ 246 views
  • Add FTP/SFTP in Visual Studio Code 183 views
  • Using Third-Party Libraries in Codeigniter 143 views
  • Open a URL in a new tab using JavaScript 141 views
  • Upload Multiple Images and Store in Database using PHP and MySQL. 130 views
  • Laravel Image Intervention Tutorial With Example 125 views
  • Hierarchical Tree view Category Example in Laravel 114 views
  • Spout, an awesome library for reading and writing in Excel. 103 views
  • Manually Install APK Files In Android Studio Emulator 98 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube

Copyright © 2021 · TutsPlanet Gene Theme on Genesis Framework · Powered By BunnyCDN