About Codeigniter, Tips & Tricks

About Codeigniter, Tips & Tricks
5 (100%) 1 vote

Why to use CodeIgniter?

CodeIgniter is a php framework, with a small footprint, based on model-view-controller(MVC) architecture, but very flexible and permissive with regard to organizing the code.

Codeigniter tips & tricks

Create a custom Codeigniter 404 page not found

As I explained in his articol, How to deal with 404 error pages, best practices is a good option to create a custom 404 error page not found.

How to create a custom Codeigniter 404 page not found?

1. In application/config/routes.php modify the field 404_override to point to your controller:

$route['404_override'] = 'my404'; (My404 is the controller).

2. Inside application/controllers create a new controller named My404

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

class My404 extends CI_Controller 
 public function __construct() 

 public function index() 
    $this->load->view('my404');//loading the custom error view

3. In application/views create the custom error view file, my404.php

Codeigniter pass value from controller to view with ajax request and json

For example a jQuery Read More link

Will add the value of the variable setted in the controller in a paragraph.

In View

<p><span id="texthere"></span><a href="#" id="clicker">Click me</a></p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    $(document).ready(function() {
            $('#clicker').click(function() {
                    url: '<?php echo base_url()?>my_controller/function_name()',
                    type: 'POST',
                    dataType: 'json',
                    success: function ( data) {
			var text_to_add = data['text_to_add'];
                        $( "#texthere" ).text(text_to_add);

In controller

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

class My_controller extends CI_Controller{
	public function function_name() {
		$data = array('text_to_add' => 'Lorem ipsum dolor sit amet, delectus detraxit partiendo eos ne, verterem consectetuer reprehendunt at has');
		echo json_encode($data);

Leave a Reply

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