Build your First Web 2.0 App with PHP, Ajax and CodeIgniter

CodeIgniter is an awesome tool if you want to learn PHP or start to use object oriented programming (OOP) with your new website. CodeIgniter is flexible with a small footprint and offers tons of features already built in, such as session management, active record use with mysql databases. The best part is it lets you use Model-View-Controller (MVC) programming. This way, your styling (view) is separate from your content (generated by controller) and lets you manipulate your raw data with a model.

There are two awesome video tutorials on CodeIgniter to show you how powerful and easy CodeIgniter can be when starting a website; however the first time I tried setting up an application I had trouble linking to images, css, and javascripts and setting up an htaccess file for friendly URLs. I have included a set of directions to help with these issues in your first CodeIgniter app.

I am working on a mac so if you have one also the tutorial may be a bit easier to follow. This tutorial also assumes you have a running apache web server with PHP and MySQL installed.

You will need a few files to start with your CodeIgniter app. You can download them from their websites below or download the zip file full I have compiled with everything you need (including CodeIgniter 1.5.4 and full compiled mootools). If you download my zip file with all the necessary components, just drag the files to a new directory in your server folder and you can skip right to developing a controller and view.

  • If you don’t have CodeIgniter already, download it at codeigniter.com.
  • If you want to use ajax, download the javascript file at mootools.net. There are also some really cool demos of mootools in action. The best part is mootools is object oriented just like CodeIgniter is! It really is a simpler way to program.
  • If you want to use friendly URLs, such as http://your-host-name/blog or http://your-host-name/contact, you will need an htaccess file, which helps in redirecting browser queries to the right place. Otherwise, your URLs will have index.php stuck in the middle of them (http://your-host-name/index.php/contact).

Now that you have CodeIgniter, drag the index.php file and system folder to a new project folder in your hosting root. I named my project folder c3. In this folder, I have created a few folders: images, _js, css, and uploads. Remember the names of these folders because you will need them for your .htaccess file.

CodeIgniter Project View in Mac OS X

In order to use friendly URLs with CodeIgniter, you need to set up an .htaccess file in your project folder. On my Mac and the web server I use, the file is invisible, but if you open your project folder with textmate you will be able to create the file and access it later. Inside this is what I have successfully used on my mac for rerouting to work.

Options +FollowSymLinks
RewriteEngine on
#RewriteCond $1 !^(index\.php|images|css|_js|uploads|robots\.txt)
#RewriteRule ^(.*)$ /~danielerrante/bizwidgets/index.php/$1 [L]

Creating an .htaccess file will solve a lot of headaches later down the road. This enables your website to get photos, javascripts and css files from the appropriate folders while still re-routing page calls.

htaccess file in textmate

Next, you want to create a controller. This will handle any processing you may require before sending a view to your visitor. Here, I have a controller called home.php

You need to create a class that extends Controller like this:

class Home extends Controller {

function Home() {
parent::Controller();
}

function index() {
$this->load->view('home_view', $data);
}

}

Now, you have a working controller. Next we will make a view for the home controller. Since we want to load the view ‘home_view’, we are going to create home_view.php in the views folder.

View in CodeIgniter

As you can see in the top of the HTML code, I like to create a header (and footer further down) for each one of my pages. Then I call the header and footer at the top and bottom of each page. This way, If I need to change something in the header, such as the page title or meta tags, I can do it on the header file once.

If you visit http://your-host-name/home your page should be working.

And, that’s it. Have fun with CodeIgniter. The forums are a great resource when you get stuck. Also check out the forum on mootools. The user guide included with CodeIgniter will also help you understand MVC programming and give you more instructions on how to use CI to its full advantage.

Thanks for reading! Tell me what you think!

Come back soon for Part 2 on MooTools.



148 Responses to “Build your First Web 2.0 App with PHP, Ajax and CodeIgniter”

  1. [...] Another great thing is when you create a website with CI, you automatically use friendly URLs (if you use an .htaccess file). If you have a bunch of unrelated pages however, you might wonder how you can keep those URLs as [...]

  2. [...] up. You can start building CI apps in about 10 minutes after watching the CI tutorials and reading my tutorial on getting set up. If you are new to Object Oriented Programming, you can read my introduction to [...]

  3. [...] In order to create dynamic, interactive pages you need to learn a programming language such as PHP. PHP is used in conjunction with a MySQL database if you need one to provide more functionality such as an online store, blog, or just for capturing visitor information. I use a PHP framework called CodeIgniter that takes care of the basics when it comes to building websites such as session handling, database connetion and data retrieval, and also provides you more organization when building websites. If you want to learn more about CodeIgniter, read my article about building your first PHP web2.0 app. [...]

  4. ganesh says:

    ezauth application is awsome it is working perfectly in local systems but it is not working main server.actual usage of .htaccess file please tell me

  5. Important info for anybody who wants one way backlinks for no cost. Anyone want free one way backlinks for their webite? I thought I would distribute some awesome info I found recently. Free one way backlinks for your website. I have been taking advantage of this this for my websites and it absolutely works great! Click my name to get what I mean. Not selling anything, it’s completely free of charge and it works.

  6. affillaaninna says:

    comprar viagra ipxdvmhkjq pvtsjssmdx viagra tdindgpjvk fyhetljfod comprar viagra online zfntliwjuc slasrjbtih

  7. I found this blog while I was searching msn on key term web host. I like articles posted. Thanks

  8. Nice post. Making quality comments are important. Make quality blog comments. That

  9. Dalton Dazzi says:

    Wow, great post. Thanks for having the guts to say it like i is.

  10. Ned Baumgarn says:

    I have a small clip from the Paris Hilton movie on my blog. To bad it got leeched onto the internet for everyone to view. She did not want it to be released as it was her ex Rick Salomon that released it.

  11. Howdy How are you today? It’s just that i dig your blog so much, and that i think you can start earning a bunch of loot from it. I run a few successful sites that I started earning some cash from recently. They are using a thing called a content blocker widget, it makes guests fill out a small quiz to unlock premium content or to download programs or tools that they need. And each time they do a easy quiz i earn about two dollars. Pretty awsome eh? Been making lots more from this than adsense.. Feel free to email me, or you can check it out through my link. tinyurl.com/yevwfst, Regards, Hennrik M. Hanson

  12. Wills says:

    I’m scared that the Pit bull needs a particular kind of proprietor…these pet dogs, no matter how ‘trusting’ nevertheless have teeth, are nevertheless creatures without moral principles and if they DO bite, won’t allow go. As in all animals…some tend to be much more suseptable to instinctual behavior and time and time again, this breed tends to try and do just that.

  13. Thank you to those who offered advice. Is is a game that you have to fight in or can die if you do something wrong? Big Fish Games Fan.

  14. Hi, i’ve been browsing around your blog and it looks really great. I’m create a family home page and struggling to make it look good. How hard was it to create your home page? Could someone like me, an amateur make that? I always wanted to write in my site something like this. Just FYI your home page seem broken when I visiting using Mozilla.

  15. Hi,season 4 has ended strange because Rita is dead and I’m waiting for season 5.I think season 5 will be the best of the seasons.

  16. [...] Another great thing is when you create a website with CI, you automatically use friendly URLs (if you use an .htaccess file). If you have a bunch of unrelated pages however, you might wonder how you can keep those URLs as [...]

  17. mudança says:

    your article help me a lot for my job.

  18. i google and found your blog,it is so good

  19. Terrific article! I entirely agree along with you.

  20. yo, i adore miss hilton. she is sexy and i cannot wait to see what she does next! x

  21. Panama says:

    I am brand-new to blogging and actually enjoyed your blog. I am going to bookmark your blog and keep checking you out. Thanks for sharing your site.

  22. This is my first time i visit right here. I discovered so numerous interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I’m not the only one having all the enjoyment right here! maintain up the good work.

  23. This article helped me a lot. It helped me to be informed and more aware. Thank you very much for the new infos and advices.

  24. iso psp says:

    Recently after upgrading to GEN D3, I have been faced with an annoying (but not critical) bug were the X and O buttons swap. On the XMB it is normal where X is to confirm and O is to cancel, but as soon as the game launches, they swap and I don’t know why.

Leave a Reply