Codeigniter Tutorials
- What is codeigniter?
- Application_Architecture
- MVC Architecture
- HMVC Architecture
- Codeigniter Configuration
- Remove index.php from url in codeigniter
- MVC Concept
- View
- Alternate PHP Syntax for View Files
- Routing
- Codeigniter URL
- Get Current URL
- Previous page URL get
- Seo Friendly URL
- Slug Create in codeigniter
- What is _remap() function
- Remove controller name from url in codeigniter
- Codeigniter Controller Class
- Class Constructor
- GET $ POST method in Codeigniter
- Models
- Basepath, Apppath, FCPATH
- URI Segment
- Page Redirect
- Helper class
- Custom Helper class
- Form Helper
- Common Helper Functions
- Common Function
- Array Problems
- Call controller in Helper
- Add active class to menu using Helper class
- Custom Library
- Custom Library Example
- when to use get_instance()
- Codeigniter Hook
- how to work inline css in codeigniter
- Custom 404 page
- 404 custom error page
- Create custom config file in codeigniter
- How to set and get config item value
- How to Speed Up CodeIgniter App?
- Codeigniter Functions
- Session
- cookies
- How to Set & Get Tempdata in Codeigniter
- flash messages in Codeigniter
- Flashdata
- Encryption and Decryption In CodeIgniter
- Codeigniter security
- csrf token form security
- Password Hashing
- Form Validation
- Custom Validation
- Registration Form with validation
- Server Side Form Validation
- Validate Select Option Field
- Date Format Validation
- Date Format change in codeigniter
- Date Functions
- DOB Validation
- CI CRUD
- User SignUp
- User Login
- User Logout
- Login Account
- Login form with RememberMe
- Login Form with session
- User change password
- Change Password with Callback Validation to Check Old Password
- Forgot password
- Reset password
- Insert data in database
- Fetch data from database
- Update data in database
- Delete data in database
- File Upload
- Image Upload with resize Image
- Upload Multiple file and images
- Upload Multiple images with CRUD
- File and image update
- Upload Image Using Ajax.
- Email Send
- Email Send Using Email library
- Email Send Using SMTP Gmail
- Notification send
- store data in json format in DB
- Json parse
- Fetch data Using Ajax with Json data
- How to Show data Using Ajax with Json parse
- Get JSON Data from PHP Script using jQuery Ajax
- Insert data Using Ajax
- Submit data Using Ajax with form validation
- How to show data Using Ajax in codeigniter
- Insert & Update Using Ajax
- Registration Form With Validation Using Ajax in codeigniter
- Delete data Using Ajax Confirmation
- Delete All data Using checkbox selection
- Ajax CSRF Token
- Ajax Post
- Ajax serverside form validation
- Contact form using AJAX with form validation
- DataTable Using Ajax dynamically
- DataTables pagination using AJAX with Custom filter
- DataTables AJAX Pagination with Search and Sort in codeigniter
- DataTables in Codeigniter using Ajax
- Ajax Custom Serarch
- Ajax Live Data Search using Jquery PHP MySql
- Ajax Custom Serarch and sorting in datatable
- Dynamic Search Using Ajax
- Autocomplete using jquery ajax
- Jquery Ajax Autocomplete Search using Typeahead
- Dynamic Dependent Dropdown Using Ajax
- Dynamic Dependent Dropdown list Using Ajax
- Dynamic Dependent Dropdown in codeigniter using Ajax
- ajax username/email availability check using JQuery
- Check Email Availability Using Ajax
- Data Load on mouse scroll
- Ajax CI Pagination
- Pagination in codeigniter
- Ajax Codeigniter Pagination
- email exists or not using ajax with json
- CRUD using AJAX With Modal popup in CI
- Add / Show Data on modal popup using Ajax
- Modal popup Validation using Ajax
- Data show on Modal popup Using Ajax
- Add / Remove text field dynamically using jquery ajax
- How to Add/Delete Multiple HTML Rows using JavaScript
- Delete Multiple Rows using Checkbox
- Multiple Checkbox value
- Form submit using jquery Example
- REST & SOAP API
- Multi-Language implementation in CodeIgniter
- How to pass multiple array in view
- Captcha
- create zip file and download
- PhpOffice PhpSpreadsheet Library (Export data in excel sheet)
- data export in excel sheet
- Excel File generate in Codeigniter using PHPExcel
- Dompdf library
- tcpdf library
- Html table to Excel & docs download
- CI Database Query
- Database Query
- SQL Injection Prevention
- Auth Model
- Join Mysql
- Tree View in dropdown option list
- OTP Integration in codeigniter
- curl post
- download file using curl
- Sweet Alert
- Sweet alert Delete & Success
- Log Message in Codeigniter
- Menu & Submenu show dynamically
- Set Default value in input box
- Cron Jobs
- Stored Procedure
- Display Loading Image when AJAX call is in Progress
- Send SMS
- IP Address
- Codeigniter Tutorialspoint
- Website Link
- How To Create Dynamic Xml Sitemap In Codeigniter
- Paypal Payment Integration
- Get Latitude and Longitude From Address in Codeigniter Using google map API
- How To Create Simple Bar Chart In Codeigniter Using AmCharts?
- dynamic Highcharts in Codeigniter
- Barcode in Codeigniter
- Codeigniter Interview Questions
- Project
What is a Controller?
A Controller is simply a class file. that is named in a way that can be associated with a URI.
Example :
itechtuto.com/Gmax_Admin/home
here home is the controller class of that url. In the above example, CodeIgniter would attempt to find a controller named Home.php and load it.
When a controller’s name matches the first segment of a URI, it will be loaded.
create a simple controller file Home.php and put the following code in it:
File name : Home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
//$this->load->view('login');
// echo 'Hello World!';
redirect('admin/Login');
}
}
Then save the file to your application/controllers/ directory.
Note : The file must be called 'Home.php', with a capital 'H'.
Note : Class names must start with an uppercase letter.
always make sure your controller extends the parent controller class so that it can inherit all its methods.
Methods :-
In the above example the method name is index(). The “index” method is always loaded by default if the second segment of the URI is empty. Another way to show your “Hello World” message would be this:
http://itechtuto.com/Home/index/
The second segment of the URI determines which method in the controller gets called.
<?php
class Home extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
echo 'Look at this!';
}
}
How to get controller name & Method name
public function index()
{
$controller = $this->router->fetch_class();
$method = $this->router->fetch_method();
print_r($controller);
print_r($method);
exit;
}
Passing URI Segments to your methods
If your URI contains more than two segments they will be passed to your method as parameters.
For example, let’s say you have a URI like this:
http://itechtuto.com/products/shoes/sandals/123
Your method will be passed URI segments 3 and 4 (“sandals” and “123”):
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
Remapping Method Calls :-
Second segment of URI determines which method is being called. If you want to override it you can use _remap() method.
method named _remap which allows you to overwrite the behavior of calling your controller methods over URI.
If you have mentioned _remap() method in your controllers, it will always get called even if URI is different. It overrides the URI.
Now think about the following factors:
CodeIgniter permits you to override its default routing behavior through the use of the _remap() function. As you already knew the second segment of the URI typically determines with function is in the controller get called and it will be passed as a parameter to the _remap() function. So to hide the real method name in the URL you can use _remap() and you can define your own set of routing rules.
If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior. in which the URI determines which function is called, allowing you to define your own function routing rules. For example: your URL is http://itechtuto.com/post/index and you do not want to call index for this then you can use _remap() to map new function view instead of index like this.
class Blog extends CI_Controller
{
public function _remap($method)
{
if ($method == 'index')
{
$this->postList();
}
else
{
show_404();
}
}
public index()
{
echo "hello index default method";
}
public function postList() {
echo "show postlist";
}
}
public function _remap($methodName)
{
if ($methodName === 'a_method')
{
$this->method();
}
else
{
$this->defaultMethod();
}
}
Any extra segments after the method name are passed into _remap() as an optional second parameter. This array can be used in combination with PHP’s call_user_func_array() to emulate CodeIgniter’s default behavior.
public function _remap($method, $params = array())
{
$method = 'process_'.$method;
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
class Blog extends Controller
{
function _remap( $method )
{
// $method contains the second segment of your URI
switch( $method )
{
case 'about-me':
$this->about_me();
break;
case 'successful':
$this->display_successful_message();
break;
default:
$this->page_not_found();
break;
}
}
function index()
{
// ---
}
function about_me()
{
// ---
}
function display_successful_message()
{
// ---
}
function page_not_found ()
{
// ---
}
function secure_method()
{
// ---
}
function Blog()
{
parent::Controller();
}
}
Processing Output :
CodeIgniter has an output class that takes care of sending your final rendered data to the web browser automatically. In some cases, however, you might want to post-process the finalized data in some way and send it to the browser yourself. CodeIgniter permits you to add a method named _output() to your controller that will receive the finalized output data. It is also responsible for caching your web pages, if you use that feature.
Note : If your controller contains a method named _output(), it will always be called by the output class instead of echoing the finalized data directly. The first parameter of the method will contain the finalized output.
Note : This class is initialized automatically by the system so there is no need to do it manually.
Here is an example:
public function _output($output)
{
echo $output;
}
Please note that your _output() method will receive the data in its finalized state. Benchmark and memory usage data will be rendered, cache files written (if you have caching enabled), and headers will be sent (if you use that feature) before it is handed off to the _output() method. To have your controller’s output cached properly, its _output() method can use:
if ($this->output->cache_expiration > 0)
{
$this->output->_write_cache($output);
}
If you are using this feature the page execution timer and memory usage stats might not be perfectly accurate since they will not take into account any further processing you do. For an alternate way to control output before any of the final processing is done,