You can find the Source code of this article here.
Howdy Guys?This time I'll bring you another awesome PHP framework.CodeIgniter you heard it don't you? Well in this tutorial I am going to discuss basic configuration we need to have in CodeIgniter and then we move on to create a simple CRUD application.
It is a cool little framework. It is so popular among the developers because of it's simple and easy structure which provide a way to build basic operations fast and with less coding.Also it's a good framework for developers who familiar with PHP but getting start on a PHP framework for the first time.
Install the CodeIgniter if you don't have done it already.It's a really simple process and follow this if you need any help.
Now we'll look at file structure inside CodeIgniter. There is 3 main folders and index.php file.
- application - This is where we create and update all the application specific changes.
- system - This contains the core of the Framework.
- user_guid - This contains the documentation of the CodeIgniter.
- index.php - serves as the front controller, initializing the base resources needed to run CodeIgniter. Also it allows you to change the name and path of the application and sustem folders.
Well not so much crux there ay? Just a simple configuration setting that any developer can understand.
It's time to create our first Controller. Go to application/controller folder and create Customer.php file.
What it does is create a new model by extending CI_Model and load the database library.So that you have the database class available via $this->$db object.
Then we'll create a method to get list of all the customer detail or one specific customer detail.
Here we use CodeIgniter's Query Builder.It make it possible to write your query once and make it use on all the available databases.To get good idea about Query Builder I recommend you to read this article.
$query = $this->db->get('customer'); line is same as select * from customer
and this
$query = $this->db->get_where('customer', array('username' => $username)); is same as
select * from customer where username='$username';
Pretty simple, Isn't it. Ok Now we are going to create the controller.Go to application/controller and create Customer.php file.
Here we create a Controller call 'Customer' and extend CI_Controller class.This controller is what become the center of your every request to your app.
Then the __construct() method: It calls the constructor of its parent class (CI_Controller) and loads the model, so it can be used in all other methods in this controller.
index method, First it invoke the get_customer method then assign a title to the page and finally load the view.
The view method,Unlike index method it pass $username as a argument and same as index method it calls the get_customer method,assign a value to the $title and load the view page.
Now lets create two view pages for these two method.Go to application/view folder and create a folder name customer inside that create a index.php file for index method.
Then create a view.php file for view method.
Since we are in view section we'll create another two pages for create and edit purpose.create a create.php file.
Then create edit.php file.
form_open function is use to render the form element and it's provide by form helper.form_validation is use to report errors related to form validation.
Go back to Customer.php and create a new method name create and add below code.
First we load the form helper which is use by form_open and then we load the form validation library.After loading those two we use set_rules method to add form validation rules.
$this->form_validation->set_rules('username', 'Username', 'required');
First argument is for name of the input field, second one is for the name that should use in error message and last one is the rule.
after form validation and if it's according to the rules we set method will invoke the set_customer method.
Now lets create edit method.
It's almost same as create method.only difference is,It take $username as argument and call get_customer method to show the initial values of the customer that you select and it invoke the update_customer method.
Since we are done with model methods we'll create set_customer method and update_customer methods.Open the Customer_model.php and create set_customer method.
Here we first take POST values and store it in $data array and we insert it to the customer table.
Finally we'll add the delete functionality to our app.In Customer_model.php create a method name delete_customer and add below code.Inside Cutomer.php file create method call delete and put below code segment.
Guys we are almost over only thing left if create proper routing configuration.If you look at .htascces file inside application folder.It deny all the....
So we'll create a new .htaccess file in our root folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then go to application/config and open config.php file.change
open routes.php file inside application/config folder change the $route arrays values as below
$config['index_page'] = 'index.php';Ok.Now we set the rules for routes.
to
$config['index_page'] = "";
open routes.php file inside application/config folder change the $route arrays values as below
$route['customer/index'] = 'customer/index';
$route['customer/create'] = 'customer/create';
$route['customer/view'] = 'customer/view';
$route['customer'] = 'customer';
$route['(:any)'] = 'customer/index';
$route['default_controller'] = 'customer/index';
I guess it's simple to understand and if you want to know more about CodeIngiter routing reffer this article.
So if you open your browser and go to
'Your_server'/root/customer or 'Your_server'/root/customer/index
will navigate you to the customer index(list) page and if you click any (View/Edit/Delete) link on Customer List it will bring you to the that particular customer operations respectively.
Note
- I have add two more view files in source code project. header.php and footer.php. As name suggest those two will serve as header and footer for the each page.
- I created another view file success.php to show a success message when ever operation is finished without errors.
- I used bootstrap CSS and update font end html tags accordingly, to have a finished product.
- 2:19:00 PM
- 9 Comments