You are previewing CodeIgniter 1.7 Professional Development.
O'Reilly logo
CodeIgniter 1.7 Professional Development

Book Description

Become a CodeIgniter expert with professional tools, techniques and extended libraries

  • Learn expert CodeIgniter techniques and move beyond the realms of the User Guide

  • Create mini-applications that teach you a technique and allow you to easily build extras on top of them

  • Create CodeIgniter Libraries to minimize code bloat and allow for easy transitions across multiple projects

  • Improving navigation and optimizing result preparation

  • Utilize third-party APIs (Twitter oAuth and Facebook Connect) and make use of some less-known helper functions – CodeIgniter's hidden gems

  • A step-by-step, practical guide with examples and screenshots

  • In Detail

    CodeIgniter is an open source PHP framework with a small footprint and exceptional performance. It gives you a rich set of libraries for common tasks, with a simple interface to access them. There are several unexplored aspects of CodeIgniter that can help developers build applications more easily and quickly. In this book, you will learn the intricacies of the framework and explore some of its hidden gems.

    If you want to get the most out of CodeIgniter, this book is for you. It teaches you what you need to know to use CodeIgniter on a daily basis. You will create mini-applications that teach a specific technique and let you build on top of the base.

    This book will take you through developing applications with CodeIgniter. You will learn how to make your CodeIgniter application more secure than a default installation, how to build large-scale applications and web services, how to release code to the community, and much more. You will be able to authenticate users, validate forms, and also build libraries to complete different tasks and functions.

    The book starts off introducing the framework and how to install it on your web server or a local machine. You are introduced to the Model-View-Controller design pattern and how it will affect your development. Some important parts of the CodeIgniter Style Guide are included to keep CodeIgniter development as standardized as possible; this helps greatly when working as part of a team or taking on an old CodeIgniter project. You will quickly move on to how CodeIgniter URLs work and learn about CodeIgniter-specific files such as helpers and plugins. By the time you finish this book, you will be able to create a CodeIgniter application of any size with confidence, ease, and speed.

    This guide will enable you to become well-versed with CodeIgniter through practical applications using the tools and techniques used by many seasoned CodeIgniter developers.

    Table of Contents

    1. CodeIgniter 1.7 Professional Development
      1. CodeIgniter 1.7 Professional Development
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. Who this book is for
        3. Conventions
        4. Reader feedback
        5. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. Getting Started with CodeIgniter
        1. Upgrading from older versions of CodeIgniter
        2. Downloading and installing CodeIgniter
          1. Getting the CodeIgniter files
        3. Introducing Model View Controller (MVC)
        4. Controllers: The business logic
          1. Defining a default Controller
          2. Remapping function calls
        5. Models: Data abstraction layer
          1. Loading a Model
          2. Connecting to your database automatically
        6. Views: Your template files
          1. Loading a view
          2. Loading multiple views
          3. Adding dynamic data
          4. Creating loops
          5. Returning views as data
        7. Autoloading resources
        8. Formatting your code—PHP Style Guide
          1. PHP closing tag
            1. Incorrect
            2. Correct
          2. Class and method naming
            1. Incorrect
            2. Correct
            3. Incorrect
            4. Correct
          3. Variable names
            1. Incorrect
            2. Correct
          4. Commenting
            1. Poor
            2. Good
          5. Use of TRUE, FALSE, and NULL
            1. Incorrect
            2. Correct
          6. Short opening tags
            1. Incorrect
            2. Correct
          7. One statement per line
            1. Incorrect
            2. Correct
        9. CodeIgniter URLs
          1. Removing the index.php file
          2. Adding a URL Suffix
        10. CodeIgniter specific files
          1. Helpers
            1. Loading a helper
              1. Loading multiple helpers
            2. Using a helper
            3. "Extending" Helpers
          2. Plugins
            1. Loading a plugin
            2. Loading multiple plugins
            3. Using a plugin
          3. Libraries
            1. Creating your own libraries
              1. Using CodeIgniter resources within your library
              2. Using your class
            2. Extending core libraries
              1. Loading your extended library
            3. Replacing core libraries
        11. Summary
      7. 2. Learning the Libraries
        1. What is a library?
        2. What do libraries do?
        3. Benchmarking Class
          1. Setting a benchmark
          2. Setting multiple benchmarks
          3. Profiling your benchmarks
            1. Making use of the Profiler Class
          4. Display total execution time
          5. Display memory consumption
        4. Input and Security Class
          1. XSS filtering
            1. Filtering a single item
            2. Automatic filtering
            3. Filtering images
            4. Retrieving POST data
            5. Retrieving GET data
            6. Retrieving GET and POST data
            7. Retrieving COOKIE data
            8. Retrieving SERVER data
            9. IP Addresses
            10. Retrieving a user agent
        5. Email Class
          1. Send an e-mail
          2. Build a contact form
            1. Our contact form
            2. Checking if the form has been submitted
            3. Checking the values of the form
            4. Validate the e-mail
            5. Using the XSS filter
            6. Send the e-mail
              1. Using the Email Class
              2. Using the Email Helper
          3. Batch e-mail processing
        6. File Uploading Class
          1. Create the Upload Views
          2. Create the Upload Controller
            1. Our initial controller
            2. Specify the allowed file types and maximum file size
            3. Uploading the file
        7. Image Manipulation Library
          1. Initializing the library
          2. Creating a thumbnail
          3. Cropping an image
          4. Rotating an image
          5. Clearing values in loops
        8. Pagination Class
          1. Example
          2. Customizing the pagination
          3. Customize the "First" link
          4. Customize the "Last" link
          5. Customize the "Next" link
          6. Customize the "Previous" link
          7. Customize the "Current Page" link
          8. Customize the "Digit" link
        9. Session Class
          1. Initializing the Session Class
            1. Autoload the Session Class
            2. Manually load the Session Class
          2. How do CodeIgniter sessions work?
          3. What is stored in a session?
          4. Adding session data
          5. Retrieving session data
          6. Removing session data
            1. Removing a single session item
            2. Removing multiple session items
            3. Destroying a session
          7. Flash data
            1. Add flashdata
            2. Read flashdata
            3. Keep flashdata
          8. Using a session database
        10. Unit testing a class
          1. Initializing the class
          2. Running tests
          3. Generating reports
          4. Strict mode
          5. Enabling or disabling unit testing
          6. Create a template
        11. Summary
      8. 3. Form Validation and Database Interaction
        1. Why should I validate my forms?
        2. Using the Form Validation Library
          1. The form validation process
            1. The user's process
            2. The development process
          2. Contact form
            1. Loading the assets
            2. Setting the rules
              1. List of validation rules
              2. Prepping data
              3. The rules
            3. Check the validation process
            4. Sending the email
            5. Final controller code
          3. Changes to the form view
            1. Re-populating field values
            2. Showing individual errors
            3. Final form view
            4. Changing the error delimiters
              1. Changing delimiters globally
              2. Changing delimiters individually
        3. Saving sets of validation rules to a config file
          1. Creating sets of rules
          2. Calling a specific set of rules
          3. Associating a rule group with a controller
        4. Using callbacks
          1. Include the callback in the rules
          2. Creating the callback
            1. Create the function
            2. Load the database library
            3. Performing the database query
            4. Adding a condition
            5. Show a success page
        5. Database interaction
          1. Loading the library
        6. Performing simple queries
        7. Returning values
          1. Returning a result object
          2. Returning a result array
          3. Returning a row object
          4. Returning a row array
        8. Result helper functions
          1. Number of rows returned
          2. Number of fields returned
          3. Free result
        9. Active Record
          1. Selecting data
            1. $this->db->get();
            2. $this->db->get_where();
            3. $this->db->select();
            4. $this->db->from();
            5. $this->db->join();
            6. $this->db->where();
              1. Single key or value method
              2. Multiple key or value method
              3. Custom key or value method
              4. Associative array method
              5. Custom string
            7. $this->db->like();
              1. Single key or value method
              2. Multiple key or value method
              3. Associative array method
            8. $this->db->group_by();
            9. $this->db->order_by();
            10. $this->db->limit();
          2. Inserting data
            1. $this->db->insert();
            2. $this->db->set();
          3. Updating data
            1. $this->db->update();
          4. Deleting data
            1. $this->db->delete();
            2. $this->db->empty_table();
            3. $this->db->truncate();
        10. Active Record caching
          1. $this->db->start_cache();
          2. $this->db->stop_cache();
          3. $this->db->flush_cache();
        11. Method chaining
        12. Manipulating databases with Database Forge
          1. Loading the Forge class
          2. Creating a database
          3. Dropping a database
          4. Adding fields
          5. Creating a table
          6. Dropping a table
          7. Renaming a table
          8. Modifying a table
            1. $this->dbforge->add_column();
            2. $this->dbforge->drop_column();
            3. $this->dbforge->modify_column();
        13. Summary
      9. 4. User Authentication 1
        1. Defining our goals
          1. Our goals
        2. Creating and connecting to the database
        3. Front end code
          1. Index function
            1. Details view
          2. Dashboard function
            1. Dashboard view
          3. Login function
            1. Form validation
              1. Password check
            2. Running the validation
            3. Full login code
            4. Login view
            5. Success view
          4. Register function
            1. Form validation
              1. User exists check
              2. Email exists check
            2. Running the validation
            3. Full register code
            4. Full controller code
            5. Register view
            6. Error view
          5. Logout function
            1. Logout view
        4. Model code
          1. Model constructor
          2. Create function
          3. Login function
          4. Logged in check function
          5. Full model code
        5. Addressing some issues
        6. The Authentication Library
        7. Summary
      10. 5. User Authentication 2
        1. Using Twitter oAuth
          1. How oAuth works
          2. Registering a Twitter application
          3. Downloading the oAuth library
            1. Library base class
            2. Controller base class
            3. Library constructor
            4. Requesting user tokens
            5. HTTP function
            6. Parse function
            7. Controller index function
            8. get_authorize_URL function
            9. Main view file
            10. Change your callback URL
            11. Creating the access function
            12. The view file
            13. Getting access tokens
              1. get_access_tokens()
              2. parse_access()
            14. Logging out
            15. Debug function
            16. Final library code
            17. Final controller code
        2. Using Facebook Connect
          1. Registering a Facebook application
          2. Download the Client library
          3. Our CodeIgniter Wrapper
            1. Base class
            2. Class constructor
            3. _session();
            4. Final library code
          4. The Controller
          5. The View file
        3. Summary
      11. 6. Application Security
        1. CodeIgniter's defenses
          1. URI security
            1. Why does CodeIgniter use URIs?
            2. Why does CodeIgniter restrict URI characters?
          2. Global data
          3. Best practices
            1. Filtering data
            2. Validation
            3. Escaping data
        2. Strong password policies
          1. Example Form Validation Library rules
        3. Storing passwords securely
          1. Storing hashes
            1. Hashing a password using sha1
              1. The sha1() function
              2. The $this->encrypt->sha1() function
                1. Load the library
                2. Hash the password
          2. Static salting
            1. Simple salts
            2. Complex salts
              1. Split at second character
              2. Split the password in the middle
          3. Dynamic salting
            1. Simple dynamic salts
            2. Complex dynamic salts
          4. Encrypting passwords
            1. Setting an encryption key
            2. Message length
            3. Loading the class
            4. Encoding passwords
            5. Decoding passwords
            6. Set mcrypt cipher
        4. Database security
          1. Escape queries
            1. The $this->db->escape() function
            2. The $this->db->escape_str() function
            3. The $this->db->escape_like_str() function
            4. Query bindings
          2. Limiting account access
        5. Cross-site scripting (XSS)
        6. Changing the default file structure
        7. Staying up-to-date
        8. Summary
      12. 7. Building a Large-Scale Application
        1. Running benchmarks
          1. Why run benchmarks?
          2. Where should I use benchmarks?
          3. Types of bottlenecks
            1. Database
              1. Identifying a database bottleneck
              2. Rectifying a database bottleneck
            2. Code
              1. Identifying a code bottleneck
              2. Rectifying a code bottleneck
            3. API
              1. Identifying an API bottleneck
              2. Rectifying an API bottleneck
            4. Bandwidth
              1. Identifying a bandwidth bottleneck
              2. Rectifying a database bottleneck
            5. Static
              1. Identifying a static bottleneck
              2. Rectifying a static bottleneck
        2. Caching
          1. How does it work?
          2. How long should I cache pages for?
          3. Using caching
        3. Optimize SQL queries
          1. Query caching
            1. To cache or not to cache?
            2. How query caching works
            3. Managing cache files
            4. Using query caching
            5. Deleting cache files
            6. Deleting all cache files
          2. Limit results
            1. LIMIT clause
            2. Only select what you need
        4. Avoid disk activity
          1. Memcache example
          2. Memcache CodeIgniter library
            1. Constructor
            2. Data functions
            3. Maintenance functions
            4. Full library code
            5. Using the library
        5. Run multiple application instances
          1. Advantages
          2. Disadvantages
        6. Summary
      13. 8. Web Services
        1. What is a web service?
        2. Types of web service
          1. Remote procedure call
          2. Representational State Transfer
        3. RESTful web service
          1. Defining the resource
          2. How it will work
          3. Creating our files
          4. Setting up the database
          5. REST library
            1. Base class
            2. Performing a GET request
            3. Performing a PUT request
            4. Performing a POST request
            5. Performing a DELETE request
            6. Response function
            7. The format XML function
            8. The format JSON function
            9. Final library code
          6. Rest Controller
            1. Base class
            2. The _remap() function
          7. Server Controller
            1. Base class
            2. The post_get() function
            3. The post_put() function
            4. The post_delete() function
            5. The post_post() function
            6. Final server controller code
          8. Post Model
            1. Base class
            2. The get_post() function
            3. The update_post() function
            4. The create_post() function
            5. The delete_post() function
            6. Final post model code
        4. Create a new post
        5. Update a post
        6. Delete a post
        7. Get a blog post
        8. Summary
      14. 9. Extending CodeIgniter
        1. Hooks
          1. Hook points
            1. pre_system
            2. pre_controller
            3. post_controller_constructor
            4. post_controller
            5. display_override
            6. cache_override
            7. scaffolding_override
            8. post_system
          2. Our hook
            1. Enable hooks
            2. Define a hook
            3. Our hook
        2. Extended controllers
          1. Admin controller
        3. Extended libraries
          1. The library
            1. Base class
            2. userdata()
            3. set_userdata()
            4. unset_userdata()
            5. Full library code
        4. Custom libraries
          1. The features
          2. The library
          3. The controller
        5. Helpers
        6. CodeIgniter's hidden gems
          1. doctype()
          2. Array helper
            1. element()
            2. random_element()
          3. Inflector helper
            1. singular()
            2. plural()
            3. camelize()
            4. underscore()
            5. humanize()
          4. highlight_code()
          5. directory_map()
        7. Summary
      15. 10. Developing and Releasing Code to the Community
        1. Find a need in the community (or fill your own need)
        2. Search for existing solutions
          1. Use the solutions
          2. Reinvent the wheel
          3. List the problems of these solutions
        3. Create a plan
          1. List the features
          2. Prioritize the features
        4. Writing the library
          1. Review your code
            1. Comments
          2. Fix the problems
        5. Write a user guide
          1. Explain the function
          2. Show the user how to use the function
          3. Let the user know of any shortcomings
          4. Use the style sheet given in the CodeIgniter User Guide
        6. Release options
          1. Forum post
          2. Post on your website
          3. GitHub
        7. Handling database tables
          1. Include a .sql file
          2. Include an install file
        8. Supporting the code
          1. Update regularly
          2. Listen to the users
          3. Accept contributions
          4. Communicate regularly
          5. Don't give a release date
          6. Keep testing
          7. Archive old versions
          8. Use the wiki
        9. Summary