Post #13
CodeIgniter (Acronym CI) is an open-source rapid development web framework, for use in building dynamic web portals with PHP. CI journey was started in 2006 when Rick Ellis created this awesome, powerful and fastest PHP framework on top of Expression-engine.
Expression-engine is collection of refactored Classes written by Ellislab programmers. Purpose of writing CodeIgniter was to develop something simple and elegant toolkit, enabling rapid development of both web sites and web applications, attracting thousands of talented PHP developers.
<?php
class PowerTutorial extends CI_Controller
{
## Benchmarking Codeigniter 3
public function benchmarkTest(){
$this->benchmark->mark('start');
$j = 0;
for($i = 0; $i<=5000;$i++){
$j++;
}
$this->benchmark->mark('end');
echo $this->benchmark->elapsed_time('start', 'end');
}
}
?>
Benchmark Result : 0.0010
isn't this interesting mark ?
this is the power i am talking about. 5000 rpm and it didn’t even take a second. i love CodeIgniter because its not just Fast but powerful enough to manage billion dollar project and trust me it takes just one line of code to write 50 lines of same in Core PHP.
the purpose of this article is to make sure you are ready to build your web application using framework instead of traditional Core PHP pattern. well let's make some curry out of this article..lol…
starting with MVC fundamental through Libraries, Helpers, Autoload & security easy configuration. all these would be followed through a simplest CMS application using Bootstrap
PHP has rich numbers of framework but choosing Codeigniter is your good luck if you want to make your client happy and connected forever. performance is unbeatable as you can see the very first benchmark codeblock i wrote above.
we are programmer and ofcourse you must know the term MVC. every programming language these days has this pattern for web and desktop app development and PHP is not virgin too you can ofcourse go with Core PHP approach and every CMS and Frameworks are also written in Core so why Codeigniter ?
Everyone knows debugging is twice as hard as writing a program in the first place right. So if you’re as clever as you can be when you write it, how will you ever debug it
Suppose you are working with team of UI developer and some PHP developer on large project. you are following Core and its going well. within few days you and your team developed some great web app and suddenly client sends message that they want registration module to have more fields and replace existing ugly captcha with reCaptch.
question arise like this: where is the registration.php page ? what, i never created this file, i used signup.php, designer, where did you put your HTML file and how can i add more fields there? and lots more that just lead you to mess. to overcome this type of mess, some developers introduced modular approach and hence we see Frameworks and CMS.
ofcourse codeigniter is all about Object oriented programming style but you don’t have to worry because its a different style here. easy one infact. Codeigniter provides abstraction for different development layers into MVC. all your database query goes to models. all html files are in Views & main logic file are all stored inside Controllers.
Controller manages both views and model files and decides which controller with which views to be served with help of Routes and without (you can simply call controllerName/yourmethodname in browser and it works)
All Right! its time for some action play actually Code Play now. lets create a controller first
<?php
class cms extends CI_Model{
}
#filepath : application/controllers/cms.php
make sure your controller name matches the file name(centralDataStorage .php). this is all you need to create your first controller. now add first method to that controller. index method is default to every controller.
<?php
class CMS extends CI_Controller
{
public function index(){
echo "This is dashboard";
}
}
#filepath : application/controllers/cms.php
now lest fire it up. open chrome or any browser you love. hit below URL. http://localhost/your_folder_name/cms
URL pattern : its very much simple in codeigniter. [controllerName/methodName] OR if you want some custom url then most welcome to config/routes.php. suppose you dont want to share index publically and you dashboard instead when a user hits above url. this simple code works like charm.
$route['default_controller'] = 'cms';
$route['dashboard'] = 'cms/index';
#filepath : application/config/routes.php
write new codes at bottom that’s my suggestion. you can put as many routes as you wish. routing is not necessary just like in Laravel so no need to focus on it but when it comes to SEO strategy then this one is important. every feature are available in Codeigniter and we are gonna cover all.
Fine with routing. what next ?
lets make it fast now. ok well fine. as we have controller and some routing done already so we are good to write some views. views go inside application/views/[folder(optional)]. you can either store inside folder or just inside views folder depends how passionate you are
my muaythai is as good as my PHP(leave it). see above code ? i am smart so that i always do modular and store views file separately for every controller. like i store all views file used by cms controller inside views/cms. not mandatory but a good development sign. i will be using Bootstrap during through out this article as bonus for you as you see i am using bootstrap classes in view files. now lets make our first model.
<?php
class centralDataStorage extends CI_Model{
// a beautiful model
// model name must match filename
}
#filepath : application/models/centralDataStorage.php
and bingo! we have our model ready now. make sure you create instance of Codeigniter constructor.
<?php
class centralDataStorage extends CI_Model{
public function __construct(){
parent::__construct();
}
}
#filepath : application/models/centralDataStorage.php
implementing parent constructor gives the core feature of codeigniter inside model and controller wherever you are calling it. now model has constructor and we are all set to write our first database query. before we go ahead with model, lets create our only table. we will be using codeigniter20minutes database(you are free to use angelina_jolie no issue but make sure table is same as i am using here)
CREATE TABLE `centraldata` (
`id` int(10) NOT NULL,
`name` varchar(30) NOT NULL,
`email` varchar(65) NOT NULL,
`description` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
database create and table is structured now. open config/database.php and register your database like this. around line number 78, you will see below code. modify as per the phpmyadmin setting.
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'codeigniter20minutes',
Clear i think!
now our database created, Model is ready & controller — views already created. now lets make our first method in model that will fetch all data from centraldata table.
<?php
class centralDataStorage extends CI_Model{
public function __construct(){
parent::__construct();
}
public function getAllData(){
$result = $this->db->select("*")
->from("centraldata")
->get()
->result();
## now data is queried. send it back to controller
return $result;
}
}
?>
Whoaaa! i know i know what you are thinking right now and i am sure you are not feeling well. what was that ?. you are thinking about the BOLD marked query inside getAllData() method right ?. we call that style — Method Chaining. you can write its simply like this
$result = $this->db->select("*")->from("centraldata")->get()->result();
see that. just one line of code does same thing so why need that line breaking styles ?. because i am a great Codeigniter developer. thats my answer. make every piece of code look amazing. build some passion. make difference.
** Enough for motivation. what next ? **
hehe..i was just..! anyway so above query is simple.
$this represents current instance of Codeigniter
$this->db indicates codeigniter that we want to query database using database class.
select() makes simple select command of SQL eg. SELECT * from Table. you can either pass * or name of columns you want to query.
Final output of so far we are doing here…
** Ok! so what does above model do ? **
you must remember, we just wrote a method getData in our model and same method we are calling in above code to get all data from centraldata table. query is already written in model using ARP so $data['result'] has now every single data from table.
as i mentioned above, everything you want in your view must be interpolated using $data. interpolation means passing variable from controller to views file. one thing we should do right now is to register database in Autoloader.
if we don’t then model query will throw database exception. lets do this first. open config/autoload.php and you will find something libraries register around 61 line. replace with this or simple add database in that array.
$autoload['libraries'] = array("database");
well. we have model data in controller now. lets modify view file to see how this data look like.
that’s awesome now because view has some dynamic data now. $result is interpolated data we passed it here from controller using $data. in above view code, we are just looping over that data and printing inside table. i am applying bootstrap class so that UI looks fine but you are free not use bootstrap
Note: i wrote this article 2 years ago on Link. i thought it would help newbies on this platform.
Dont forget landing your query incase something at some point bounced. i'm here to clarify your queries!
Thanks for Reading!
Top comments (0)