I Love codeigniter, You?


Published on

Published in: Technology
1 Comment
  • Hey genius, when you use someone else's materials you are supposed to give proper attribution, not remove the slide with the presenter's information. It's called plagiarism. http://www.slideshare.net/nicdev/5-reasons-to-love-codeigniter-5122075
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Devotion to framewroks is almost like sports teams or religion. In truth you should pick the one that better suits your needs and style, not the one that has the most hype.
  • How many of you tried to start using a framework and were frustrated by how it forced you to change your coding style?
  • The controller will interpret the first segment after the controller name (the second segment) as a variable passed to the function. You can pass as many variables as you need using this method, but careful, if you require the variables and they aren’t present you’ll get an error as usual.Explain how to pass a second var
  • Enter the URI class. The URI class let’s you pick and choose from the URI segments, and do other cool manipulations. The URI class is autoloaded, so you can call it without declaring it first.By default uri_to_assoc starts at segment 3 assuming the first two are controller/function
  • Who hates validating forms? It’s tedious, and error prone. CI comes with a bazillion functions to help you deal with forms, but here are the basics.This is a normal form with two added functions. Validation_errors() and set_value()
  • What’s happening here? When we call the form controller it loads the index function by default.We load the form_validation library.Set validation rules for the username field. Notice the field name, human-friendly name, validation rules (these are built in)The first time the controller runs the form hasn’t been submitted, so the validation returns FALSE and loads the ‘form_view’ view
  • Notice the human readable name is used, the error message is built in although it can be customized. The previous form entry is auto-populated.
  • Data prepping, Use any native PHP function that takes a single argument.Run your own validation functionsAdd your own delimiters. By default each error message is wrapped in <p> tagsDisplay errors individually as opposed to all in a list. This could allow you putting the error next to the field.No only text fields can be auto-populated, select, checkbox and radio inputs can be returned to the state they were submitted too.
  • CI doesn’t use $_GET at all (it can be tricked into doing so, but it’s unnecessary)If you are a knuclehead and turned it on for some reason, it won’t matterthe values are escaped when interfacing with the DB and through the XSS filterThere’s overhead involved in using XSS filter globally, I didn’t benchmark it or notice it.
  • If you want to learn how the inner workings are, check the input class
  • Bindings automatically escape queriesActive record takes care of all securityProtect identifiers puts backticks on field and table namesEscape like is for LIKE db calls
  • Base URL is the page where the links need to be directed toTotal rows is usually determined dynamically based on the number of items to display. Presumably by querying the database and finding out the size of the result set.Per page is the number of items to showThis is the minimum configuration required
  • Robert Swarthout presented image manipulation libraries back in February, so if you saw that, this is a nice continuation.
  • During development I like to add FirePHP, autoload it and use it as $this->firephp->log(‘output to console’)
  • I Love codeigniter, You?

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