Using Composer with Codeigniter

So you want to use composer in your next Codeigniter Project?

Well here’s how to do it:

1. Download and install Codeigniter

2. CD to project directory and install Composer
curl -sS https://getcomposer.org/installer | php

3. Create a composer.json file in your project root and define dependencies
{
"require": {
"monolog/monolog": "1.2.*"
}
}

4. Install Dependecies
php composer.phar install

This will create a vendor directory in your root folder of your project.

5. Add autoload.php to your Codeigniter’s index.php before require_once BASEPATH.’core/CodeIgniter.php’;

include_once './vendor/autoload.php';

Thats’s it. Now you can use your newly installed libraries in CI. Have fun!

Validating URLs

How many times have you struggled with URL validations? I have struggled a lot. Damn, those stupid regex(s). Well regex as a tech is not stupid, it’s awesome. but when u have a 10,000 regex expressions to choose from? and on the top of that every regex gives different result.

When I faced this challenge, I tested more than 80+ regex expressions, obviously not in a day :P. Then had to settle with a regex which had a least probability of producing -ve results.

Obviously as a programmer, I wanted better alternative. And I stumbled upon PHP filters. Well, it can be used for other things than validation as well such as sanitization, etc.

Why I would like to use filter_var instead of my or anybody else’s regex. Simply because, regex used in filter_Var are well tested and produce far less -ves. Moreover, being a native function, it produces results faster.

As far as my Codeigniter Application is concerned, I have extended the Validation library:

And I am all set *grins*.

Codeigniter 2.0

So finally Codeigniter 2.0 is here. With 2.0 a lot has changed. Primarily, Ellislab split up CI into two branches, Reactor and Core.

As they say,

CodeIgniter Core
Core is a slow-moving branch that will be used as the base for EllisLab commercial products such as ExpressionEngine and MojoMotor. It will continue at a similar pace that CodeIgniter has in the past and will be useful for large commercial applications that require the upmost in stability and backward/forward compatibility at a pace more typical of SLA backed Enterprise products. Core is publicly available with tagged downloads at BitBucket.

CodeIgniter Reactor
Reactor is a community driven branch of CodeIgniter that will enable faster adoption of the best community submitted code to the framework. This means the community can create a fork of the project on BitBucket and contribute bug fixes, new features, documentation improvements, etc and have it reviewed by a code-review team called Reactor Engineers. These Engineers are primarily responsible for driving the development of the framework.

In simple words, Reactor will be managed by community + Ellislab and Core will be managed solely by Ellislab.

Some interesting things I liked,

  • Support for PHP 4.x is gone
  • Application Folder now sits outside System folder
  • CLI support (I am still trying to figure it out)
  • Full Query String support
  • memcache and APC support

Also, Ellislab has changed the file structure a lot. Check out change log to know more.

In all, new version seems interesting. Though moving 1.7.x apps to 2.x is not easy. Since, Plugins have been deprecated, you will need to convert plugins either into libraries or helpers. Also, some of the libraries may not work with 2.x.

The only thing that is making me doubtful is why CI has been split up into 2 branches? Is Ellislab trying to make money by creating an Enterprise version? I hope they don’t.

Asset Management in CodeIgntier

This is a quick tip, I wanna share with you all.

many of you must be using CodeIgniter for development of web apps. And I know everyone of you must have faced the problem of asset management (css, JS, images) in Codeigntier. Well here’s a quick tip for it and making it hassle free.

use of base tag in head section

First of all place all your files in some folder, this is how I use it. I always create three folders viz. css, images and js in root folder of codeigniter app. Then add following in head section

<base href=”<?php echo base_url(); ?>” />

After this, u can use only partial links in all you linking relative to you base href’s property!

For eg.

<script src=”js/jquery.min.js”></script>

and thats all, You won’t have to include base_url all the time in your codes for linking. It can be used for everything like images, scripts, css files etc.

Hope you find it useful!

A Better Programming Stack!

We all programmers always try to find ways to do work more quickly and efficiently. Ask any programmer and he’ll say that “I have to write code again and again for so many things! I hate it!”. I am not talking about using for loops and functions here. I am talking about database connectivity, cross platform compatibility. I am talking about Frameworks!

In this post, I will try to emphasize on the benefits and usefulness of using development framework and how to update your development stack to next level. In short, this post is meant for those LAMP developers who still are using core PHP/CSS and HTML for development.

Frameworks

Frameworks for LAMP has been around for some time now and have evolved a lot. According to wikipedia

software framework, in computer programming, is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code providing specific functionality. Frameworks are a special case of software libraries in that they are reusable abstractions of code wrapped in a well-defined API, yet they contain some key distinguishing features that separate them from normal libraries.

PHP Frameworks

There are a lot of php frameworks to choose from, But I suggest CodeIgniter to beginners.

CodeIgniter - PHP Framework
CodeIgniter - PHP Framework

Why is it best? because it’s really easy to install and learn. Requires minimal configuration and it’s ready to use in 3 minutes. It supports all major databases. All you have to do is call it’s API and it’ll automagically generate SQL queries according to database used. It’s also supports XSS filtering, also has form validation class etc. etc.

It’s also extensible. You can create your own library classes and extend it’s core. There is an extensive documentation which covers each and every aspect of codeigntier.

Home Page: http://www.codeigntier.com/

Other Frameworks to look at

CSS Frameworks

I can list you more than 10 useful CSS Frameworks, but the one that’s very popular and my personal favorite is: BluePrintCSS.

BlurPrintCSS - CSS Framework
BlurPrintCSS - CSS Framework

CSS framework allows you to quickly create websites. This one in particular offers you a grid, typography, forms and even a print css. It’s cross browser compatible so you don’t have to worry about cross browser compatibility. All you have to do to use it is to include screen.css and that’s it. And obviously you’ll have to learn it’s classes though 😉

Homepage: http://www.blueprintcss.org/

Other Frameworks to look for

Javascript Frameworks

Javascript frameworks are awesome tools which let you do all kind of animations on the fly, you can also do ajax painlessly.

My personal favorite is: Jquery

Jquery - Javascript Framework
Jquery - Javascript Framework

If you ask me, which framework will win the JS Framework war, I’ll bet on Jquery. It’s so much fun and easy to use. You’ll never look back after using this. I bet you!

Other Javascript Frameworks to look at

Well I think that will be all to get you guys started! If you think I missed out something then do let me know! All comments are welcome