Codeigniter Controller (Examples) - Web Development and Web Design Codes

Latest

Friday, February 2, 2018

Codeigniter Controller (Examples)

Codeigniter Controller

Codeigniter Controller
Codeigniter Controller

A controller is a simple class file. As the name suggests, it controls the whole application by URI.
How to Install Codeigniter
codeigniter configuration Route:
In CI code will run without routes.php. routes.php are use to customise the url/routes like if you have url like

example.com/pages/about_us
where "pages" is your controller and "about_us" is method in that controller, if you want to show url without controller like example.com/about_us, then you need to use routes.php in this way

$route['about_us'] = ['pages/about_us'];
Configuration Route:
Go to Codeigniter/application/config/ and open routes.php 
now Find out 
without Route Your URL will Look like that : http://localhost/codeigniter/index.php/welcome
$route['default_controller'] = 'welcome';
Affter Rote URl Your URL will look like: http://localhost/codeigniter
Here 'welcome' is our Conroller Name..
You can Load Controller Function by Replacing welcome..
1.Creating Controller:
Go to Your Codeigniter/application/controllers and create welcome.php file

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

class Welcome extends CI_Controller {
  public function index()
 {
  $this->load->view('welcome_user');
 }
 public function about(){
  echo "This is About Page";
 }
 public function author(){
  echo "This is Author Page";
 }
}


Note: Welcome is Class name and The class name must start with uppercase letter but we need to write lowercase letter when we call that controller by URI. index,about and author is Method of Welcome.
$this->load->view('welcome_message');
Here we call view to load welcome_user.php page from Codeigniter/application/view/
2.Creating View:
go to Codeigniter/application/view/ and create welcome_user.php


<html>
  <head>
   <title>Codeigniter Controller Examples-- http://codenair.com</title>
  </head>
  <h1>Welcome to Codeigniter Lession</h1>
</html>
3.Calling a Controller:
The above controller can be called by URI as follows −

  1. http://localhost/codeigniter/
  2. http://localhost/codeigniter/index.php/welcome/about
  3. http://localhost/codeigniter/index.php/welcome/author
Welcome is Controller name..about and author is method of welcome..
Result:
codeigniter controller

codeigniter controller

codeigniter controller

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.