+ All Categories
Transcript
Page 1: 5 Reasons To Love CodeIgniter

5 Reasons To Love CodeIgniter

Page 2: 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]

Page 3: 5 Reasons To Love CodeIgniter

Everyone Loves Their Framework

Page 4: 5 Reasons To Love CodeIgniter

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.

Page 5: 5 Reasons To Love CodeIgniter

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”

Page 6: 5 Reasons To Love CodeIgniter

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

Page 7: 5 Reasons To Love CodeIgniter

How Does it Work?It’s a thing of beauty, really.

class Presenters extends Controller{

function display($var){echo $var;

}

}

//Echoes “nic”

Page 8: 5 Reasons To Love CodeIgniter

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.

Page 9: 5 Reasons To Love CodeIgniter

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>

Page 10: 5 Reasons To Love CodeIgniter

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'); }

}}

Page 11: 5 Reasons To Love CodeIgniter
Page 12: 5 Reasons To Love CodeIgniter

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()

Page 13: 5 Reasons To Love CodeIgniter

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.

Page 14: 5 Reasons To Love CodeIgniter

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;

Page 15: 5 Reasons To Love CodeIgniter

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);

Page 16: 5 Reasons To Love CodeIgniter

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();

Page 17: 5 Reasons To Love CodeIgniter
Page 18: 5 Reasons To Love CodeIgniter

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

Page 19: 5 Reasons To Love CodeIgniter

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;

Page 20: 5 Reasons To Love CodeIgniter

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();

Page 21: 5 Reasons To Love CodeIgniter

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

Page 22: 5 Reasons To Love CodeIgniter
Page 23: 5 Reasons To Love CodeIgniter

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!

Page 24: 5 Reasons To Love CodeIgniter

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/


Top Related