5 Reasons To Love CodeIgniter
description
Transcript of 5 Reasons To Love CodeIgniter
5 Reasons To Love CodeIgniter
Who Is This Guy?
Nic Rosental, web developer and owner of coworking space Cobb Exchange.
You can find me in all of these placeshttp://nicolasrosental.com@[email protected]
Everyone Loves Their Framework
What Is CodeIgniter?
• It’s an MVC framework.• MVC stands for Model View Controller and it’s
a logical way of organizing an application. – Models interact with the database– Controllers take care of the logic– Views present the output
• CodeIgniter doesn’t force you to do things this way, it enables you to do so.
Why CodeIgniter?
According To MeMost frameworks will help you write better code
faster. CodeIgniter will help you do that without forcing you into some intricate, and
weird way of doing things.
According To EllisLab (creators of CI)“…helps you write kick-ass PHP programs”
Submit URIs To Your Will
One of the foundations of CI is “clean URLs”. Nice looking, well organized URLs, without any of the
ugliness of query strings.Ugly
atlantaphp.org/presenters.php?action=display&name=nic
Prettyatlantaphp.org/presenters/display/nic
How Does it Work?It’s a thing of beauty, really.
class Presenters extends Controller{
function display($var){echo $var;
}
}
//Echoes “nic”
Need More Control?
Why yes, there’s a class for thatatlantaphp.org/archives/show/picture/1234
$this->uri->segment(3); //picture$this->uri->segment(99,0); //Returns 0 instead of FALSE$this->uri->uri_to_assoc(); //[array](‘picture’ => ‘1234’)$this->uri->uri_string(); // /archives/show/picture/1234$this->uri->total_segments(); //4
There are several more functions to play around with.
Form and Data Validation
The Form view
<?php echo validation_errors(); ?>
<form action="form_test" method="post”>
<label for="username">Username</label><input type="text" name="username" value="<?php echo set_value('username'); ?>"/><input type="submit" value="Submit" />
</form>
The controllerclass Form_test extends Controller {
function index(){ //Index is always the default for the controller
$this->load->library('form_validation'); $this->form_validation->set_rules('username',
'Username', 'required|min_length[5]');
if ($this->form_validation->run() == FALSE) {
$this->load->view('form_view’); } else {
$this->load->view('form_ok'); }
}}
Other Cool Stuff$this->form_validation->set_rules('username', 'Username',
'trim|required|min_length[5]’);
$this->form_validation->set_rules('username', 'Username', ‘check_duplicate');
$this->form_validation->set_error_delimiters(’<span class="error">', '</span>');
<?php echo form_error('username'); ?>
set_select() set_checkbox() and set_radio()
Security and XSS Filtering
CodeIgniter does a few things right off the bat.Destroys $_GET.Destroys all global variables (although
register_globals is off by default since 4.2.0)Filters $_POST and $_COOKIE array keys, allowing
only alphanumeric characters plus “~%.:_\-”XSS filtering can be enabled globally or called by a
function.
XSS Filtering
Can be enabled two ways
Locally: $data = $this->input->xss_clean($data);
$img_file = this->input->xss_clean($file, TRUE) //Checks image files for XSS attacks
$name = $this->input->post(‘name’, TRUE) //$_POST[‘name’]
Globally (in the config file):config['global_xss_filtering'] = TRUE;
Database SecurityThere are many ways to sanitize your queries, CodeIgniter
offers a few.
Bindings$sql = “SELECT FROM members WHERE name = ?”;$this->db->query($sql, array(‘nic’));
Using the Active Record class takes care of security aspects$this->db->where(’name', $name);$this->db->update(’users', $data);
Other Functions$this->db->protect_identifiers('table_name');$this->db->escape($data);$this->db->escape_str($data);$this->db->escape_like_str($data);
PaginationIt’s always an issue to display large data sets in an orderly manner.
CodeIgniter provides a way to set up proper pagination in a few lines of code.
In the controller
$this->load->library('pagination');
$config['base_url'] = 'http://atlantaphp.org/pager/index/';$config['total_rows'] = 200;$config['per_page'] = 10;
$this->pagination->initialize($config);
In the view
echo $this->pagination->create_links();
Image Manipulation
CodeIgniter does four image processing types very easily: crop*, resize, rotate, watermark.
Create a thumbnail$config['image_library'] = 'gd2';$config['source_image'] = 'assets/images/elephpant.jpg';$config['create_thumb'] = TRUE;$config['maintain_ratio'] = TRUE; //Saved from a P.I.T.A$config['width']= 75;$config['height’] = 50;
$this->load->library('image_lib', $config); $this->image_lib->resize();
*Never got crop to work
Rotate
$config['image_library'] = 'gd2';$config['source_image'] =
'assets/images/elephpant.jpg';$config['new_image'] =
'assets/images/elephpant_rotate.jpg';$config['rotation_angle'] = '180';$config['rotation_angle'] = 'hor';
$this->load->library('image_lib', $config);
$this->image_lib->rotate;
Watermark
$config['source_image'] = 'assets/images/elephpant.jpg';$config['new_image'] = 'assets/images/elephpant_wm.jpg';
$config['wm_text'] = 'ATLANTAPHP ROCKS!';$config['wm_type'] = 'text';$config['wm_font_path'] = 'assets/images/tesox.ttf';$config['wm_font_size'] = '25';$config['wm_font_color'] = 'ffffff';$config['wm_vrt_alignment'] = 'top';$config['wm_hor_alignment'] = 'center';$config['wm_padding'] = '10';
$this->load->library('image_lib', $config);
$this->image_lib->watermark();
Errors and Cleaning House
The image manipulation functions return boolean values, so you can display errors on FALSE
if( ! $this->image_lib->watermark()){
echo $this->image_lib->display_errors();
}
Clean up after yourself!$this->image_lib->clear(); //Clears the config values
More To Love
• Extremely easy to set up.• Intuitive (i.e. short learning curve.)• It’s open source and has a permissive license.• Extend existing libraries and add your own.• Load only what you use.• The community is outstanding. • It has a Smiley helper!
Resources
• CodeIgniter User Guide• http://codeigniter.com/user_guide/• CodeIgniter Forums• http://codeigniter.com/forums/• CodeIgniter From Scratch (Nettuts video series)• http://net.tutsplus.com/articles/news/codeigniter-from-
scratch-day-1/• Forrst – Not a CI dedicated site, but many CI users belong
to the community, and the service itself is built on CI.• http://forrst.com/