Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting started with PHP framework codeigniter

DZone's Guide to

Getting started with PHP framework codeigniter

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

I have always worked with core PHP and was always reluctant to use any CMS (Joomla, Drupal etc). Coincidentally I never got a chance to work on any of the frameworks in PHP. But in the current project at work, we have decided to use CodeIgniter.

CodeIgniter is a light weight open source web application framework. In short, it gives you a directory structure of a proper MVC pattern along with a light weight built-in templating engine of its own. MVC pattern helps developer to separate business logic and the presentation layer. It requires very minimal setup, unlike the famous PHP framwork, Zend. Zend is mostly used in developing enterprise applications and is a vast framework, hence making the learning curve more difficult. On the other hand, CodeIgniter is easy to setup, and is more easy to learn.

Let’s Get started with CodeIgniter

Of course the first step is to download the codeigniter framework. You can download the latest stable version of codeigniter from the following url http://codeigniter.com/downloads/ . It will be a compressed file (zip file) whcih will be downloaded, just unzip it and keep it in a folder, say codeigniter. Make sure you keep it inside the htdocs or webserver’s directory.

Not its time to configure the codeigniter which initial settings to start working on. To do so, open the file config.php in to your favorite editor. The file resides inside system/application/config directory. You will see an array called $config which stores all the configuration information, we will change only the following values and leave the rest as it is.

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
*/
//don't forget the TRAILING slash (/)
$config['base_url']	= "http://localhost/codeigniter/";

Trust me, that shall be all to get you started. Some blogs/sites have show much changes in this file, but you can play around with them when needed. You can change a lot about your application form this single file. Like you can store the session information inside a database, by simple setting up the config var $config['sess_use_database'] as TRUE, which is FALSE by default. You can also change the name of the session table from default “ci_sessions” to anything you like.

Since none of the web application runs without database, so let me show you how to configure database in this setup. I decided to pick this up in next tutorial, but since its pretty straight forward, let’s do it. To configure database, open database.php which is located in the same directory as of config.php i.e. system/application/config . Now change the values of the following array keys with the actual values of your MySQL server.

$active_group = "default";
$active_record = TRUE;
 
$db['default']['hostname'] = "localhost";
$db['default']['username'] = ""; //database username
$db['default']['password'] = ""; //database password
$db['default']['database'] = ""; //name of the database
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = ""; //any table prefix, if you want to keep one
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

 

As you see the database configuration was also simple. Now its time to develop a simple application using the CI framework. At the very beginning of this post I mentioned that CodeIgniter gives you a MVC pattern structure, so every part has a separate folder. Let us create a simple controller class to start with.

To do this, lets create a class called Hello which will be written in the file called hello.php, stored inside system/application/controllers/ folder. The content of the file can be : (you can have different class, after all its just a PHP class).


class hello extends Controller
{
  function  __construct()
  {
    //call parent constructor
    parent::__construct();
  }
 
  function index()
  {
    $output['header'] = "My page's header";
    $output['content'] = "Here is the content of the webpage";
    //load from from the system/application/views/hello.php
    $this->load->view('hello',$output);
  }
}

Dissect the code

Now let us understand the code line by line. Its a controller class called hello which extends the default parent class called Controller. Extending the Controller class facilitates the execution of this controller. The constructor of this calls simply calls loads the constructor of the parent class, which apparently is Controller. Next is the index(); function which has a special meaning in CI framework. If its present in a controller, it is executed automatically when the controller is loaded/executed.

Now we output the data using the view part of the MVC pattern. if you notice the above code loads a view called hello which is a file kept inside system/application/views/ folder. The file name should be same as the name of the controller. And the data is provided in the form of an array, where header,content  are the template variables, as show below in the hello.php

<html>
  <head>
    <title>My first CodeIgniter Controller and View</title>
  </head>
  <body>
    <h1><?php echo $header ?></h1>
    <p><?php echo $content ?></p>
  </body>
</html>

 

The above view renders the data which is passed on by the calling controller. Now lets try and test if everything works as we expected, phew ! Open your browser and hit

http://localhost/codeigniter/index.php/hello/ 

If everything is well, you should see a webpage, with a header and body content as specified in the controller. Wonder, how did that got render ? CodeIgniter provides a very nice routing mechanism based on REST. The URL is so that you can call a specific controller and a method from inside the controller. But if you notice, in the above URL we have mentioned only the controller name i.e. hello and not the function name index(). As mentioned previously, This is due to the fact that index(); function gets called automatically if it exists. Might it have been another function called render then the above URL would have been changed to

http://localhost/codeigniter/index.php/hello/render/

The above URL which now call the render(); function from the hello controller.

 

CodeIgniter was easy, no ? If you think that was tough, just go through the steps again and I am sure you will understand. Since CI is the most easiest framework and is lets you get started very quickly with the development.

Happy Development !

Stay Digified !!

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Sachin Khosla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}