Building web applications with PHP is a pretty simple process. As a PHP developer, you can develop practically anything, including database abstraction layers, form validation programs, file upload applications, and so forth. The list goes on and on.
Once you've mastered the foundations of the object-oriented paradigm and learned how to implement certain common design patterns in real-world conditions, you should be armed with a useful toolkit comprised of all sorts of classes and functions that can be used any number of times.
Although you may not be aware of this fact, your toolkit can be considered a general PHP development framework. The end result of your own efforts and skills as programmer.
However, there are certain situations when the capabilities offered by your own framework simply aren’t good enough to fit the requirements of a particular application. In a case like this, you can either upgrade your framework, which will probably require coding additional classes and functions, or you can pick up a third-party software from the numerous packages available on the web today.
If you choose the latter, then you’ll be confronted with another challenge that will keep your mind spinning in circles. Should I pick up a full-featured framework like Zend, or should I work with a more approachable solution, like CakePHP or WACT?
Actually, the answer to that question depends strongly on what type of project you’re going to develop and how much time you want to spend configuring and learning how to use a particular framework. For instance, Zend is great for building enterprise-level PHP applications, but it's likely that you’ll find it pretty overwhelming to work with at first.
On the other hand, CakePHP can be much easier to set up and use, even though they’re not loaded with all the features offered by Zend. However, in the last few months, a small framework has gained popularity with many PHP programmers due to its ultra-fast learning curve and easy configuration.
It's a solid piece of software written by Rick Ellis in PHP 4. It permits users to develop small and middle-scale PHP applications in minutes (literally) with minimal setup. In addition, CodeIgniter is built around the Model-View-Controller pattern, which allows you to easily separate data from application logic and visual presentation.
Downloading CodeIgniter
You can download the latest and greatest version of CodeIgniter by going to
www.codeigniter.com and clicking the Download CodeIgniter button on the home page.
Now that you have CodeIgniter downloaded and unzipped, take a minute to look at the file structure.
As received, the CodeIgniter root directory contains two files and two folders. The two files are index.php (which you want to keep there no matter what) and a license.txt file, containing CodeIgniter ’s license agreement. The two directories are user_guide/ (containing a version of the online
documentation at www.codeigniter.com) and system/.
A quick note before you delve too much further into that all-important system/ folder: Approximately 100 percent of your web projects will require the use of custom CSS, JavaScript, and image files and folders. The best way to handle all of these assets is to create folders for them at the project root, as
siblings to the system/ folder. This is as good a time as any to create your css/, js/, and images/ folders, so do that now. To make those folders accessible to CodeIgniter, you will also need to create a small .htaccess file in the root folder of your web site.
The system/ Folder
❑ application — The application folder contains the application you’re building. Basically, this folder contains your models, views, controllers, and other code (like helpers and class extensions). In other words, this folder is where you’ll do 99 percent of your work.
❑ cache — The cache folder contains all cached pages for your application.
❑ codeigniter — The codeigniter folder is where CodeIgniter ’s core classes live. You have almost no reason to go in here. All of your work will occur in the application folder. Even if your intent is to extend the CodeIgniter core, you would do it with hooks, and hooks live in the application folder.
❑ database — The database folder contains core database drivers and other database utilities. Again, there’s no good reason for you to be in this folder.
❑ fonts — The fonts folder contains font-related information and utilities. Again, there’s no reason to spend any time here.
❑ helpers — The helpers folder contains standard CodeIgniter helpers (such as date, cookie, and URL helpers). You’ll make frequent use of helpers in your CodeIgniter career and can even extend helpers thanks to improvements introduced in CodeIgniter version 1.6.
❑ language — The language folder contains language files. You can ignore it for now.
❑ libraries — The libraries folder contains standard CodeIgniter libraries (to help you with e-mail, calendars, file uploads, and more). You can create your own libraries or extend (and even replace) standard ones, but those will be saved in the application/libraries directory to keep them separate from the standard CodeIgniter libraries saved in this particular folder.
❑ logs — The logs folder is the folder CodeIgniter uses to write error and other logs to.
❑ plugins — The plugins folder contains plugins. Plugins and helpers are very similar, in that they both allow developers to quickly address an issue or create content (like forms, links, etc.). However, the main difference between them is that plugins usually consist of one function, while helpers often have many functions bundled inside them.
Initial Configuration
we should know the ins and outs of the files inside the system/application/config/ folder.
❑ config.php
❑ database.php
❑ autoload.php
❑ routes.php
config.php
The config.php file contains a series of configuration options (all of them stored in a PHP array called, appropriately enough, $config) that CodeIgniter uses to keep track of your application’s information and settings. The first configuration option you need to set inside config.php is the base URL of your application.
The second thing you need to do is set a value for your home page by editing the $config[‘index_page’] configuration option. CodeIgniter ships with a value of “index.php” for this option, which means that index.php will appear in all of your URLs. Many CodeIgniter developers prefer to keep this value blank.
CodeIgniter ’s Global XSS Filtering option is set to FALSE by default. The online User Guide suggests that setting this to TRUE adds a lot of performance overhead to the system. However, at this point, it is better to have some global protection put in place. That way you can be assured of some security
precautions while you’re in development.
In the same security vein, notice that sess_encrypt_cookie has been set to TRUE, and that you are to enter a 32-character encryption salt in encryption_key. Doing these two things will encrypt any sessions and provide a salt for any hashing methods you use. Be sure to use a random string of upper-
and lowercase letters and numbers.
One final note before moving on: Make sure that you write down your encryption key and keep it safe somewhere, or, at least, maintain good backups. You’ll need the key to retrieve other information, so if your site is compromised or erased (or if you lose your key any other way), you’ll be glad you have a
record of it.
database.php
To connect to your database, simply enter valid information for your hostname, username, password, database name, and database driver.
autoload.php
The autoload.php file specifies which systems are automatically loaded by CodeIgniter.
The first $autoload option, libraries, is a list of libraries that should be loaded. You’re going to be using the database, session, e-mail, and form validation libraries a lot in this (and many other) CodeIgniter project(s), thus loading them now makes a good deal of sense. You learn more about these libraries later.
The second $autoload option, helper, is a list of helper collections that you will need to get work done. Almost every CodeIgniter project uses the URL, form, and text helpers frequently; it’s not a bad idea to have date and security autoloaded as well, particularly as you’ll need them to help you parse dates and handle security features.
For the time being, keep the third $autoload option, plugin, either blank or filled in with the CAPTCHA plugin, as you may have a need to use CAPTCHA devices in forms for your application for extra security.
routes.php
The routes.php file lets you remap URI requests to specific controller functions.The default_controller route tells CodeIgniter which controller should be loaded if no controller is identified.
CodeIgniter Libraries
CodeIgniter includes the following libraries:
❑ Benchmarking — The Benchmarking library is always active. Use it to determine the time difference between any two marked points in code and to calculate memory usage.
❑ Calendaring — The Calendaring library must be loaded by a controller. Use it to dynamically create calendars for given months and years, with some control over formatting and appearance.
❑ Config — The Config library is initialized automatically by the system. Use it to retrieve configuration information.
❑ Database — The Database library is a very powerful set of methods that must be loaded.
❑ Email — The Email library must be loaded. It includes a very powerful set of tools that simplifies the job of sending e-mails.
❑ Encryption — The Encryption library must be loaded. It provides you with powerful two-way encryption methods.
❑ File Uploading — The File Uploading library must be loaded. Use this library whenever you need to handle file uploads. It includes powerful validation features that can restrict a file by mime type, size (in kilobytes), or even image dimensions.
❑ FTP — The FTP library must be loaded. Use this library to transfer files to a remote server (only standard FTP is supported, by the way).
❑ HTML Table — The HTML Table library must be loaded. Use this very versatile library to autogenerate HTML tables from arrays or database result sets.
❑ Image Manipulation — The Image Manipulation library must be loaded. Use it to resize images, create thumbnails, crop or rotate images, and watermark images. Some functions require further PHP support (such as GD/GD2).
❑ Input and Security — The Input and Security library must be loaded. Use it to pre-process input data (from forms and URLs) and to handle some security functions (such as guarding against XSS attacks).
❑ Language — The Language library must be loaded. Use this library to load different sets of language files for internationalization.
❑ Loader — The Loader library is automatically loaded. You will use this library primarily to load views with your controller, but it is also used to load libraries.
❑ Output — The Output library is automatically loaded. This library has one main function: Send the finalized web page to the requesting browser. It is also used for caching.
❑ Pagination — The Pagination library must be loaded. Use this labor-saving library to paginate database results for performance and usability. You can control how many records to display per page, how many records to pull from the database, and the look and feel of different parts of the pagination.
❑ Session — The Session library must be loaded. Use CodeIgniter ’s Session library to maintain state information about a user. This library does not use PHP’s built-in sessions — instead, it generates its own session data.
❑ Template Parser — The Template Parser library must be loaded. Use this library to create templates that contain parsable pseudo-templates. If you are familiar with Smarty, you’ll find that CodeIgniter ’s templating isn’t as feature-rich but is still very useful.
❑ Trackback — The Trackback library must be loaded. Use this library to send and receive Trackback data.
❑ Unit Testing — The Unit Testing library must be loaded. Use this class to unit test functions in your application. CodeIgniter provides an evaluation function and two result functions in this library.
❑ URI Class — The URI Class library is loaded automatically. You’ll use this library a good deal, especially when you’re parsing URLs, breaking them into segments that can then be passed to a controller or saved as variables.
❑ User Agent — The User Agent library must be loaded. Use this library to identify the browser, mobile device, or robot visiting your site. You can also use it to detect supported languages, character sets, and even referrers.
❑ Validation — The Validation library must be loaded. Use this library to validate form input in your applications.
❑ XML-RPC — The XML-RPC library must be loaded. Use this library to set up XML-RPC clients and servers.
❑ Zip Encoding — The Zip Encoding library must be loaded. Use this library to create Zip archives of both text and binary data.
CodeIgniter Helpers
Helpers, as their name implies, help you with specific tasks. Unlike libraries, helpers are not object-oriented but procedural in nature. Each helper contains one or more functions, each focusing on a specific task, with zero dependence on other functions.Helpers can either be loaded locally or autoloaded in /system/application/config/autoload.php.
CodeIgniter ships with the following built-in helpers:
❑ Array — The Array helper contains functions that help you work with arrays. For example, the random_element() function takes an array as input and returns a random element from it.
❑ Cookie — The Cookie helper contains functions that help you set, read, and delete cookie data.
❑ Date — The Date helper contains functions that help you work with dates. For example, the now() function returns the current time as a UNIX time stamp.
❑ Directory — The Directory helper contains a single function that helps you work with directories. For example, the directory_map function reads a specified directory path and builds an array of it that contains all of its files and subdirectories.
❑ Download — The Download helper contains a single function that helps you download data easily. The force_download() function generates server headers that force data to be downloaded instead of viewed in a browser.
❑ File — The File helper contains functions that help you read, write, and delete files.
❑ Form — The Form helper contains functions that help you build forms. It is probably one of the most used helpers in the CodeIgniter toolbox.
❑ HTML — The HTML helper contains functions that help you create HTML blocks quickly and easily. For example, the ul() function can turn an array of items into a bulleted list.
❑ Inflector — The Inflector helper contains functions that help you to turn words into plural or singular form, to apply camel case, or to turn words separated by spaces into an underscored phrase. For example, the singular() function can turn the string “girls” into “girl.”
❑ Security — The Security helper contains security-related functions like xss_clean(), which filters out any code that may be used in a cross site scripting hack.
❑ Smiley — The Smiley helper contains functions that help you manage emoticons. The functions in this helper might seem superfluous, but become invaluable if you are coding a bulletin board or chat application.
❑ String — The String helper contains functions that help you work with strings, like the random_ string() function, which as its name implies, creates random strings based on type and length arguments.
❑ Text — The Text helper contains functions that help you work with text. For example, the word_ limiter() function can limit a string to a certain number of words, which is useful if you’re trying to limit user input on a form.
❑ Typography — The Typography helper contains a single function that helps you format text in appropriate ways.
❑ XML — The XML helper contains a single function that helps you work with XML. The xml_ convert() function converts a given string into XML-ready text, converting ampersands and angle brackets (among other things) into entities.
No comments:
Post a Comment