DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Laravel for Beginners: An Overview of the PHP Framework
  • Power of PHP Frameworks: Boosting Web Development With PHP Frameworks
  • Maximizing Laravel's Potential: A Guide to Driver-Based Services
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT

Trending

  • MySQL to PostgreSQL Database Migration: A Practical Case Study
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • The Full-Stack Developer's Blind Spot: Why Data Cleansing Shouldn't Be an Afterthought
  • Metrics at a Glance for Production Clusters
  1. DZone
  2. Coding
  3. Frameworks
  4. Getting started with PHP framework codeigniter

Getting started with PHP framework codeigniter

By 
Sachin Khosla user avatar
Sachin Khosla
·
Aug. 12, 22 · News
Likes (1)
Comment
Save
Tweet
Share
11.0K Views

Join the DZone community and get the full member experience.

Join For Free

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 lightweight open source web application framework. In short, it gives you a directory structure of a proper MVC pattern along with a lightweight built-in templating engine of its own. MVC pattern helps developers to separate business logic and the presentation layer. It requires a very minimal setup, unlike the famous PHP framework, 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 set up and easier to learn.

Using Proper Frameworks

It is so very important to apply the proper frameworks to any project that you are working on. When you use the best coding frameworks, you can accomplish a lot more than you might have ever dreamed was possible.

Eliminating some of the noise and distractions that can take you away from truly accomplishing all that you need to is a big step in the right direction, and you will find that getting started with the right frameworks is probably what you need to do as a first step towards that goal. Otherwise, you will find yourself floundering for the right answers, and your projects just won’t turn out the way that you might have hoped that they would.

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) that 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.

Now it's time to configure the Codeigniter which initial settings to start working on. To do so, open the file config.php in 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 shown many changes in this file, but you can play around with them when needed. You can change a lot about your application from this single file. Like you can store the session information inside a database, by simply 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 applications runs without database, let me show you how to configure a database in this setup. I decided to pick this up in the next tutorial, but since it's pretty straightforward, let’s do it. To configure the database, open database.php which is located in the same directory as 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 it's time to develop a simple application using the CI framework. At the very beginning of this post I mentioned that CodeIgniter gives you an MVC pattern structure, so every part has a separate folder. Let us create a simple controller class to start with.

To do this, let's 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 a different class, after all, it's 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. It's 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 call simply loads the constructor of the parent class, which apparently is Controller. Next is the index(); function which has a special meaning in the CI framework. If it's 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 the system/application/views/ folder. The file name should be the 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 shown 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 let's 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 get rendered? 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 easiest framework and it lets you get started very quickly with the development.

Happy Development!

PHP CodeIgniter Framework

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

Opinions expressed by DZone contributors are their own.

Related

  • Laravel for Beginners: An Overview of the PHP Framework
  • Power of PHP Frameworks: Boosting Web Development With PHP Frameworks
  • Maximizing Laravel's Potential: A Guide to Driver-Based Services
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!