From b0dd10f8171945e0c1f3527dd1e9d18b043e01a7 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 25 Aug 2006 17:25:49 +0000 Subject: Initial Import --- user_guide/general/alternative_php.html | 142 +++++++++++++ user_guide/general/ancillary_classes.html | 122 +++++++++++ user_guide/general/autoloader.html | 106 ++++++++++ user_guide/general/base_classes.html | 127 ++++++++++++ user_guide/general/caching.html | 119 +++++++++++ user_guide/general/changelog.html | 248 ++++++++++++++++++++++ user_guide/general/controllers.html | 317 +++++++++++++++++++++++++++++ user_guide/general/creating_libraries.html | 222 ++++++++++++++++++++ user_guide/general/credits.html | 96 +++++++++ user_guide/general/errors.html | 142 +++++++++++++ user_guide/general/helpers.html | 140 +++++++++++++ user_guide/general/hooks.html | 184 +++++++++++++++++ user_guide/general/index.html | 95 +++++++++ user_guide/general/libraries.html | 111 ++++++++++ user_guide/general/models.html | 256 +++++++++++++++++++++++ user_guide/general/multiple_apps.html | 116 +++++++++++ user_guide/general/plugins.html | 125 ++++++++++++ user_guide/general/quick_reference.html | 83 ++++++++ user_guide/general/requirements.html | 88 ++++++++ user_guide/general/routing.html | 163 +++++++++++++++ user_guide/general/scaffolding.html | 153 ++++++++++++++ user_guide/general/scripts.html | 115 +++++++++++ user_guide/general/security.html | 159 +++++++++++++++ user_guide/general/urls.html | 159 +++++++++++++++ user_guide/general/views.html | 249 ++++++++++++++++++++++ 25 files changed, 3837 insertions(+) create mode 100644 user_guide/general/alternative_php.html create mode 100644 user_guide/general/ancillary_classes.html create mode 100644 user_guide/general/autoloader.html create mode 100644 user_guide/general/base_classes.html create mode 100644 user_guide/general/caching.html create mode 100644 user_guide/general/changelog.html create mode 100644 user_guide/general/controllers.html create mode 100644 user_guide/general/creating_libraries.html create mode 100644 user_guide/general/credits.html create mode 100644 user_guide/general/errors.html create mode 100644 user_guide/general/helpers.html create mode 100644 user_guide/general/hooks.html create mode 100644 user_guide/general/index.html create mode 100644 user_guide/general/libraries.html create mode 100644 user_guide/general/models.html create mode 100644 user_guide/general/multiple_apps.html create mode 100644 user_guide/general/plugins.html create mode 100644 user_guide/general/quick_reference.html create mode 100644 user_guide/general/requirements.html create mode 100644 user_guide/general/routing.html create mode 100644 user_guide/general/scaffolding.html create mode 100644 user_guide/general/scripts.html create mode 100644 user_guide/general/security.html create mode 100644 user_guide/general/urls.html create mode 100644 user_guide/general/views.html (limited to 'user_guide/general') diff --git a/user_guide/general/alternative_php.html b/user_guide/general/alternative_php.html new file mode 100644 index 000000000..8838ac8a0 --- /dev/null +++ b/user_guide/general/alternative_php.html @@ -0,0 +1,142 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Alternate PHP Syntax for View Files

+ +

If you do not utilize Code Igniter's template engine, you'll be using pure PHP +in your View files. To minimize the PHP code in these files, and to make it easier to identify the code blocks it is recommended that you use PHPs alternative +syntax for control structures and echo statements. If you are not familiar with this syntax, it allows you to eliminate the braces from your code, +and eliminate "echo" statements.

+ +

Alternative Echos

+ +

Normally to echo, or print out a variable you would do this:

+ +<?php echo $variable; ?> + +

With the alternative syntax you can instead do it this way:

+ +<?=$variable?> + +

Note: If you find that the syntax described in this page does not work on your server it might +be that "short tags" are disabled in your PHP ini file.

+ + +

Alternative Control Structures

+ +

Controls structures, like if, for, foreach, and while can be +written in a simplified format as well. Here is an example using foreach:

+ + +<ul>
+
+<?php foreach($todo as $item): ?>
+
+<li><?=$item?></li>
+
+<?php endforeach ?>
+
+</ul>
+ +

Notice that there are no braces. Instead, the end brace is replaced with endforeach. +Each of the control structures listed above has a similar closing syntax: +endif, endfor, endforeach, and endwhile

+ +

Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is +important!

+ +

Here is another example, using if/elseif/else. Notice the colons:

+ + +<?php if ($username == 'sally'): ?>
+
+   <h3>Hi Sally</h3>
+
+<?php elseif ($username == 'joe'): ?>
+
+   <h3>Hi Joe</h3>
+
+<?php else: ?>
+
+   <h3>Hi unknown user</h3>
+
+<?php endif; ?>
+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/ancillary_classes.html b/user_guide/general/ancillary_classes.html new file mode 100644 index 000000000..fc67bec70 --- /dev/null +++ b/user_guide/general/ancillary_classes.html @@ -0,0 +1,122 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Creating Ancillary Classes

+ +

In some cases you may want to develop classes that exist apart from your controllers but have the ability to +utilize all of Code Igniter's resources. This is easily possible as you'll see.

+ +

get_instance()

+ + +

Any class that you instantiate within your controller functions can access Code Igniter's native resources simply by using the get_instance() function. +This function returns the main Code Igniter object.

+ +

Normally, to call any of the available Code Igniter functions requires you to use the $this construct:

+ + +$this->load->helper('url');
+$this->load->library('session');
+$this->config->item('base_url');
+etc. +
+ +

$this, however, only works within your controllers, your models, or your views. +If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:

+ + +

First, assign the Code Igniter object to a variable:

+ +$obj =& get_instance(); + +

Once you've assigned the object to a variable, you'll use that variable instead of $this:

+ + +$obj =& get_instance();

+$obj->load->helper('url');
+$obj->load->library('session');
+$obj->config->item('base_url');
+etc. +
+ +

Note: You'll notice that the above get_instance() function is being passed by reference: +

+$obj =& get_instance(); +

+This is very important. Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it.

+
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/autoloader.html b/user_guide/general/autoloader.html new file mode 100644 index 000000000..5f90fddad --- /dev/null +++ b/user_guide/general/autoloader.html @@ -0,0 +1,106 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Auto-loading Resources

+ +

Code Igniter comes with an "Auto-load" feature that permits libraries, helpers, and plugins to be initialized +automatically every time the system runs. If you need certain resources globally throughout your application you should +consider auto-loading them for convenience.

+ +

The following items can be loaded automatically:

+ + + +

To autoload resources, open the application/config/autoload.php file and add the item you want +loaded to the autoload array. You'll find instructions in that file corresponding to each +type of item.

+ +

Note: Do not include the file extension (.php) when adding items to the autoload array.

+ + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/base_classes.html b/user_guide/general/base_classes.html new file mode 100644 index 000000000..215bd25b5 --- /dev/null +++ b/user_guide/general/base_classes.html @@ -0,0 +1,127 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Replacing System Classes

+ +

Every time Code Igniter runs there are several base classes that are initialized automatically as part of the core framework. +It is possible, however, to swap any of the core system files with your own versions.  Most users will never have any need to do this, +but the option to replace them does exist for those that would like to significantly alter the Code Igniter core. +

+ +

Note:  Replacing a core system class with your own version has a lot of implications, so make sure you +know what you are doing before attempting it.

+ + +

System Class List

+ +

The following is a list of the core system files that are invoked every time Code Igniter runs:

+ + + +

Replacing Core Classes

+ +

To use one of your own system classes instead of a default one simply place your version inside your local libraries directory:

+ +application/libraries/some-class.php + +

Any file named identically to one from the list above will be used instead of the one normally used.

+ +

Please note that your class must use CI as a prefix. For example, if your file is named Input.php the class will be named:

+ + +class CI_Input {

+ +} +
+ + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/caching.html b/user_guide/general/caching.html new file mode 100644 index 000000000..a5edbe73f --- /dev/null +++ b/user_guide/general/caching.html @@ -0,0 +1,119 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Web Page Caching

+ +

Code Igniter lets you cache your pages in order to achieve maximum performance. + +Although Code Igniter is quite fast, the amount of dynamic information you display in your pages will correlate directly to the +server resources, memory, and processing cycles utilized, which affect your page load speeds. +By caching your pages, since they are saved in their fully rendered state, you can achieve performance that nears that of static web pages. + + +

How Does Caching Work?

+ +

Caching can be enabled on a per-page basis, and you can set the length of time that a page should remain cached before being refreshed. +When a page is loaded for the first time, the cache file will be written to your system/cache folder. On subsequent page loads the cache file will be retrieved +and sent to the requesting user's browser. If it has expired, it will be deleted and refreshed before being sent to the browser.

+ +

Note: The Benchmark tag is not cached so you can still view your page load speed when caching is enabled.

+ +

Enabling Caching

+ +

To enable caching, put the following tag in any of your controller functions:

+ +$this->output->cache(n); + +

Where n is the number of minutes you wish the page to remain cached between refreshes.

+ +

The above tag can go anywhere within a function. It is not affected by the order that it appears, so place it wherever it seems +most logical to you. Once the tag is in place, your pages will begin being cached.

+ +

Note: Before the cache files can be written you must set the file permissions on your +system/cache folder such that it is writable (666 is usually appropriate).

+ +

Deleting Caches

+ +

If you no longer wish to cache a file you can remove the caching tag and it will not longer be refreshed when it expires. Note: +Removing the tag will not delete the cache immediately. It will have to expire normally. If you need to remove it earlier you +will need to manually delete it from your cache folder.

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/changelog.html b/user_guide/general/changelog.html new file mode 100644 index 000000000..0bcb165da --- /dev/null +++ b/user_guide/general/changelog.html @@ -0,0 +1,248 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Change Log

+ + + +

Version 1.4.0

+

Release Date: August 25, 2006

+ + + + + +

Version 1.3.3

+

Release Date: June 1, 2006

+ + + + + + +

Version 1.3.2

+

Release Date: April 17, 2006

+ + + + + +

Version 1.3.1

+

Release Date: April 11, 2006

+ + + + +

Version 1.3

+

Release Date: April 3, 2006

+ + + + + + + + + + +

Version 1.2

+

Release Date: March 21, 2006

+ + + + + + + +

Version Beta 1.1

+

Release Date: March 10, 2006

+ + + +

Version Beta 1.0

+

Release Date: February 28, 2006

+

First publicly released version.

+ +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/controllers.html b/user_guide/general/controllers.html new file mode 100644 index 000000000..648298eb0 --- /dev/null +++ b/user_guide/general/controllers.html @@ -0,0 +1,317 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Controllers

+ +

Controllers are the heart of your application, as they determine how HTTP requests should be handled.

+ + + + + + +

What is a Controller?

+ +

A Controller is simply a class file that is named in a way that can be associated with a URI.

+ +

Consider this URI:

+ +www.your-site.com/index.php/blog/ + +

In the above example, Code Igniter would attempt to find a controller named blog.php and load it.

+ +

When a controller's name matches the first segment of a URI, it will be loaded.

+ + +

Let's try it:  Hello World!

+ +

Let's create a simple controller so you can see it in action. Using your text editor, create a file called blog.php, and put the following code in it:

+ + + + + + +

Then save the file to your application/controllers/ folder.

+ +

Now visit the your site using a URL similar to this:

+ +www.your-site.com/index.php/blog/ + +

If you did it right, you should see Hello World!.

+ +

Note: Class names must start with an uppercase letter. In other words, this is valid: + +<?php
+class Blog extends Controller {
+
+}
+?>
+ +

This is not valid:

+ +<?php
+class blog extends Controller {
+
+}
+?>
+ +

Also, always make sure your controller extends the parent controller class so that it can inherit all its functions.

+ + + + +

Functions

+ +

In the above example the function name is index(). The "index" function is always loaded by default if the +second segment of the URI is empty. Another way to show your "Hello World" message would be this:

+ +www.your-site.com/index.php/blog/index/ + +

The second segment of the URI determines which function in the controller gets called.

+ +

Let's try it. Add a new function to your controller:

+ + + + +

Now load the following URL to see the comment function:

+ +www.your-site.com/index.php/blog/comments/ + +

You should see your new message.

+ + +

Private Functions

+ +

In some cases you may not want certain functions accessible publicly. To make a function private, simply add an +underscore as the name prefix and it will not be served via a URL request. For example, if you were to have a function like this:

+ + +function _utility()
+{
+  // some code
+}
+ +

Trying to access it via the URL, like this, will not work:

+ +www.your-site.com/index.php/blog/_utility/ + + + + +

Defining a Default Controller

+ +

Code Igniter can be told to load a default controller when a URI is not present, +as will be the case when only your site root URL is requested. To specify a default controller, open +your application/config/routes.php file and set this variable:

+ +$route['default_controller'] = 'Blog'; + +

Where Blog is the name of the controller class you want used. If you now load your main index.php file without +specifying any URI segments you'll see your Hello World message by default.

+ + + +

Class Constructors

+ + +

If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it:

+ +parent::Controller(); + +

The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.

+ + +

If you are not familliar with constructors, in PHP 4, a constructor is simply a function that has the exact same name as the class:

+ + +<?php
+class Blog extends Controller {
+
+       function Blog()
+       {
+            parent::Controller();
+       }
+}
+?>
+ +

In PHP 5, constructors use the following syntax:

+ + +<?php
+class Blog extends Controller {
+
+       function __construct()
+       {
+            parent::Controller();
+       }
+}
+?>
+ +

Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. +Constructors can't return a value, but they can do some default work.

+ + +

Reserved Function Names

+ +

Since your controller classes will extend the main application controller you +must be careful not to name your functions identically to the ones used by that class, otherwise your local functions +will override them. The following +is a list of reserved names. Do not name your controller functions any of these:

+ + + +


If you are running PHP 4 there are some additional reserved names. These ONLY apply if you are running PHP 4.

+ + + + + + + + +

That's it!

+ +

That, in a nutshell, is all there is to know about controllers.

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/creating_libraries.html b/user_guide/general/creating_libraries.html new file mode 100644 index 000000000..fa3416767 --- /dev/null +++ b/user_guide/general/creating_libraries.html @@ -0,0 +1,222 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Creating Libraries

+ +

When we use the term "Libraries" we are normally referring to the classes that are located in the libraries +directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create your own libraries within +your application directory in order to maintain separation between your local resources and the global framework resources.

+ +

Storage

+ +

In order for your libraries to be stored in your application folder you will need to create two directories in which to store them:

+ + + + +

Anatomy of a Library

+ +

A class library consists of two components:

+ +
    +
  1. An init file.
  2. +
  3. A class file.
  4. +
+ +

The Init File

+ +

An init file a small initialization file corresponding to each of your classes. The purpose of this file is to +instantiate a particular class. Each init file must be named identically to your class file name, adding the "init_" prefix. For example, if your +class is named myclass.php your init file will be named:

+ +init_myclass.php + +

Within your init file you will place your initialization code. Here's an example of such code, using an imaginary class named Myclass:

+ + +<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+if ( ! class_exists('Myclass'))
+{
+     require_once(APPPATH.'libraries/Myclass'.EXT);
+}
+
+$obj =& get_instance();
+$obj->myclass = new Myclass();
+$obj->ci_is_loaded[] = 'myclass';
+
+?>
+ +

The Class File

+ +

Your class file itself will be placed inside your libraries directory:

+ +application/libraries/myclass.php + +

The class will have this basic prototype:

+ +<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+class Myclass {
+
+}
+?>
+ + +

Naming Conventions

+ + + + +

Using Your Class

+ +

From within any of your Controller classes you can initialize your class using the standard:

+ +$this->load->library('myclass'); + +

Once loaded you can access your class using:

+ +$this->myclass->function(); + +

Note: In your init file you can define the object variable name.

+ + +

Passing Parameters Your Class

+ +

In the library loading function you can pass data via the second parameter and it will be available to your initialization file:

+ + +$params = array('type' => 'large', 'color' => 'red');
+
+$this->load->library('myclass', $params);
+ +

Parameters will be accessible using a variable called $params. By default this variable is set to FALSE.

+ + +

Utilizing Code Igniter Resources within Your Library

+ + +

To access Code Igniter's native resources within your library use the get_instance() function. +This function returns the Code Igniter super object.

+ +

Normally, to call any of the available Code Igniter functions requires you to use the $this construct:

+ + +$this->load->helper('url');
+$this->load->library('session');
+$this->config->item('base_url');
+etc. +
+ +

$this, however, only works directly within your controllers, your models, or your views. +If you would like to use Code Igniter's classes from within your own custom classes you can do so as follows:

+ + +

First, assign the Code Igniter object to a variable:

+ +$obj =& get_instance(); + +

Once you've assigned the object to a variable, you'll use that variable instead of $this:

+ + +$obj =& get_instance();

+$obj->load->helper('url');
+$obj->load->library('session');
+$obj->config->item('base_url');
+etc. +
+ +

Note: You'll notice that the above get_instance() function is being passed by reference: +

+$obj =& get_instance(); +

+This is very important. Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it.

+ + + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/credits.html b/user_guide/general/credits.html new file mode 100644 index 000000000..dd318b7d4 --- /dev/null +++ b/user_guide/general/credits.html @@ -0,0 +1,96 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Credits

+ +

Code Igniter was developed by Rick Ellis, who in his other life is CEO of +pMachine, Inc. The core framework was written +specifically for this application, while many of the class libraries, helpers, and sub-systems borrow from the code-base of +ExpressionEngine, a Content Management System written by Rick Ellis and +Paul Burdick.

+ +

A hat tip goes to Ruby on Rails for inspiring us to create a PHP framework, and for +bringing frameworks into the general consciousness of the web community.

+ +

The Code Igniter logo and icons were created by Rick Ellis.

+ +

The pull-down table of contents was created with the use of the moo.fx library.

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/errors.html b/user_guide/general/errors.html new file mode 100644 index 000000000..60acbfe1f --- /dev/null +++ b/user_guide/general/errors.html @@ -0,0 +1,142 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Error Handling

+ +

Code Igniter lets you build error reporting into your applications using the functions described below. +In addition, it has an error logging class that permits error and debugging messages to be saved as text files.

+ +

Note: By default, Code Igniter displays all PHP errors. You might +wish to change this behavior once your development is complete. You'll find the error_reporting() +function located at the top of your main index.php file. Disabling error reporting will NOT prevent log files +from being written if there are errors.

+ +

Unlike most systems in Code Igniter, the error functions are simple procedural interfaces that are available +globally throughout the application. This approach permits error messages to get triggered without having to worry +about class/function scoping.

+ +

The following functions let you generate errors:

+ +

show_error('message')

+

This function will display the error message supplied to it using the following error template:

+

application/errors/error_general.php

+ +

show_404('page')

+

This function will display the 404 error message supplied to it using the following error template:

+

application/errors/error_404.php

+ +

The function expects the string passed to it to be the file path to the page that isn't found. +Note that Code Igniter automatically shows 404 messages if controllers are not found.

+ + +

log_message('level', 'message')

+ +

This function lets you write messages to your log files. You must supply one of three "levels" +in the first parameter, indicating what type of message it is (debug, error, info), with the message +itself in the second parameter. Example:

+ + +if ($some_var == "")
+{
+    log_message('error', 'Some variable did not contain a value.');
+}
+else
+{
+    log_message('debug', 'Some variable was correctly set');
+}
+
+log_message('info', 'The purpose of some variable is to provide some value.');
+
+ +

There are three message types:

+ +
    +
  1. Error Messages. These are actual errors, such as PHP errors or user errors.
  2. +
  3. Debug Messages. These are messages that assist in debugging. For example, if a class has been initialized, you could log this as debugging info.
  4. +
  5. Informational Messages. These are the lowest priority messages, simply giving information regarding some process. Code Igniter doesn't natively generate any info messsages but you may want to in your application.
  6. +
+ + +

Note: In order for the log file to actually be written, the "log_errors" +option must be enabled in your application/config/config.php file, and the "logs" folder must be writable. +In addition, you'll can set the "threshold" for logging. +You might, for example, only want error messages to be logged, and not the other two types.

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/helpers.html b/user_guide/general/helpers.html new file mode 100644 index 000000000..1bed4a8b0 --- /dev/null +++ b/user_guide/general/helpers.html @@ -0,0 +1,140 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Helper Functions

+ +

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular +category. There are URL Helpers, that assist in creating links, there are Form Helpers +that help you create form elements, Text Helpers perform various text formatting routines, +Cookie Helpers set and read cookies, File Helpers help you deal with files, etc. +

+ +

Unlike most other systems in Code Igniter, Helpers are not written in an Object Oriented format. They are simple, procedural functions. +Each helper function performs one specific task, with no dependence on other functions.

+ +

Code Igniter does not load Helper Files by default, so the first step in using +a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

+ +

Loading a Helper

+ +

Loading a helper file is quite simple using the following function:

+ +$this->load->helper('name'); + +

Where name is the file name of the helper, without the .php file extension or the "helper" part.

+ +

For example, to load the URL Helper file, which is named url_helper.php, you would do this:

+ +$this->load->helper('url'); + +

A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), +as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available +automatically in any function, or you can load a helper in a specific function that needs it.

+ +

Note: The Helper loading function above does not return a value, so don't try to assign it to a variable. Just use it as shown.

+ + +

Loading Multiple Helpers

+ +

If you need to load more than one helper you can specify them in an array, like this:

+ +$this->load->helper( array('helper1', 'helper2', 'helper3') ); + +

Auto-loading Helpers

+ +

If you find that you need a particular helper globally throughout your application, you can tell Code Igniter to auto-load it during system initialization. +This is done by opening the application/config/autoload.php file and adding the helper to the autoload array.

+ + +

Using a Helper

+ +

Once you've loaded the Helper File containing the function you intend to use, you'll call it the way you would a standard PHP function.

+ +

For example, to create a link using the anchor() function in one of your view files you would do this:

+ +<?=anchor('blog/comments', 'Click Here');?> + +

Where "Click Here" is the name of the link, and "blog/comments" is the URI to the controller/function you wish to link to.

+ + +

Now What?

+ +

In the Table of Contents you'll find a list of all the available Helper Files. Browse each one to see what they do.

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/hooks.html b/user_guide/general/hooks.html new file mode 100644 index 000000000..301654653 --- /dev/null +++ b/user_guide/general/hooks.html @@ -0,0 +1,184 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Hooks - Extending the Framework Core

+ +

Code Igniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. +When Code Igniter runs it follows a specific execution process, diagramed in the Application Flow page. +There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process. +For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of +your own scripts in some other location. +

+ + +

Defining a Hook

+ +

Hooks are defined in application/config/hooks.php file. Each hook is specified as an array with this prototype:

+ + +$hook['pre_controller'] = array(
+                                'class'    => 'MyClass',
+                                'function' => 'Myfunction',
+                                'filename' => 'Myclass.php',
+                                'filepath' => 'hooks',
+                                'params'   => array('beer', 'wine', 'snacks')
+                                );
+ +

Notes:
The array index correlates to the name of the particular hook point you want to +use. In the above example the hook point is pre_controller. A list of hook points is found below. +The following items should be defined in your associative hook array:

+ + + + +

Multiple Calls to the Same Hook

+ +

If want to use the same hook point with more then one script, simply make your array declaration multi-dimensional, like this: + + +$hook['pre_controller'][] = array(
+                                'class'    => 'MyClass',
+                                'function' => 'Myfunction',
+                                'filename' => 'Myclass.php',
+                                'filepath' => 'hooks',
+                                'params'   => array('beer', 'wine', 'snacks')
+                                );
+
+$hook['pre_controller'][] = array(
+                                'class'    => 'MyOtherClass',
+                                'function' => 'MyOtherfunction',
+                                'filename' => 'Myotherclass.php',
+                                'filepath' => 'hooks',
+                                'params'   => array('red', 'yellow', 'blue')
+                                );
+ +

Notice the brackets after each array index:

+ +$hook['pre_controller'][] + +

This permits you to the same hook point with multiple scripts. The order you define your array will be the execution order.

+ + +

Hook Points

+ +The following is a list of available hook points.

+ + + + + + + + + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/index.html b/user_guide/general/index.html new file mode 100644 index 000000000..31e99d53e --- /dev/null +++ b/user_guide/general/index.html @@ -0,0 +1,95 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Getting Started With Code Igniter

+ +

Any software application requires some effort to learn. We've done our best to minimize the learning +curve, while making the process as enjoyable as possible. +

+ +

The first step is to install Code Igniter, then read +all the topics in the Introduction section of the User Guide.

+ +

Next, read each of the General Topics pages in order. +Each topic builds on the previous one, and includes code examples that you are encouraged to try.

+ +

Once you understand the basics you'll be ready to explore the Class Reference and +Helper Reference pages to learn to utilize the native libraries and helper files.

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/libraries.html b/user_guide/general/libraries.html new file mode 100644 index 000000000..642399ba9 --- /dev/null +++ b/user_guide/general/libraries.html @@ -0,0 +1,111 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Loading Libraries

+ +

The core libraries are the class files located in the "libraries" folder. +In most cases, to use one of these classes involves initializing it within your controllers using the following function:

+ +$this->load->library('class name'); + +

Where class name is the name of the class you want to invoke. For example, to load the validation class you would do this:

+ +$this->load->library('validation'); + +

Once initialized you can use it as indicated in the user guide page corresponding to each class.

+ +

Semantic Relevance

+ +

The pattern you will use when calling functions is this:

+ +

$this->class->function()

+ +

We've placed a high value on semantic relevance in the naming of our classes and functions. +Here are some examples of function calls you might use in Code Igniter:

+ +

$this->email->send()

+

$this->input->user_agent()

+

$this->benchmark->elapsed_time()

+

$this->db->query()

+

$this->load->helper()

+

$this->uri->segment()

+

$this->lang->line()

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/models.html b/user_guide/general/models.html new file mode 100644 index 000000000..dfb45d622 --- /dev/null +++ b/user_guide/general/models.html @@ -0,0 +1,256 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Models

+ +

Models are optionally available for those who want to use a more +traditional MVC approach.

+ + + + + + + +

What is a Model?

+ +

Models are PHP classes that are designed to work with information in your database. For example, let's say +you use Code Igniter to manage a blog. You might have a model class that contains functions to insert, update, and +retrieve your blog data. Here is an example of what such a model class might look like:

+ + +class Blogmodel extends Model {
+
+    var $title   = '';
+    var $content = '';
+    var $date    = '';
+
+    function Blogmodel()
+    {
+        // Call the Model constructor
+        parent::Model();
+    }
+    
+    function get_last_ten_entries()
+    {
+        $query = $this->db->get('entries', 10);
+        return $query->result();
+    }
+
+    function insert_entry()
+    {
+        $this->title   = $_POST['title'];
+        $this->content = $_POST['content'];
+        $this->date    = time();
+
+        $this->db->insert('entries', $this);
+    }
+
+    function update_entry()
+    {
+        $this->title   = $_POST['title'];
+        $this->content = $_POST['content'];
+        $this->date    = time();
+
+        $this->db->update('entries', $this, array('id', $_POST['id']));
+    }
+
+}
+ +

Note: The functions in the above example use the Active Record database functions.

+ + + +

Anatomy of a Model

+ +

Model classes are stored in your application/models/ folder. The basic prototype for a model is this:

+ + + +class Model_name extends Model {
+
+    function Model_name()
+    {
+        parent::Model();
+    }
+}
+ +

Where Model_name is the name of your class. Class names must be capitalized. +Make sure your class extends the base Model class.

+ +

The file name will be a lower case version of your class name. For example, if your class is this:

+ + +class User_model extends Model {
+
+    function User_model()
+    {
+        parent::Model();
+    }
+}
+ +

Your file will be this:

+ +application/models/user_model.php + + + +

Loading a Model

+ +

Your models will typically be loaded and called from within your controller functions. +To load a model you will use the following function:

+ +$this->load->model('Model_name'); + +

Once loaded, you will access your model functions using an object with the same name as your class:

+ + +$this->load->model('Model_name');
+
+$this->Model_name->function(); +
+ +

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading +function:

+ + + +$this->load->model('Model_name', 'fubar');
+
+$this->fubar->function(); +
+ +

Here is an example of a controller, that loads a model, then serves a view:

+ + +class Blog_controller extends Controller {
+
+    function blog()
+    {
+        $this->load->model('Blog');
+
+        $data['query'] = $this->Blog->get_last_ten_entries();

+        $this->load->view('blog', $data);
+    }
+}
+ + + + +

Connecting to your Database

+ +

When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:

+ + + + + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/multiple_apps.html b/user_guide/general/multiple_apps.html new file mode 100644 index 000000000..229349493 --- /dev/null +++ b/user_guide/general/multiple_apps.html @@ -0,0 +1,116 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Running Multiple Applications with one Code Igniter Installation

+ +

By default it is assumed that you only intend to use Code Igniter to manage one application, which you will build in your +system/application/ directory. It is possible, however, to have multiple sets of applications that share a single +Code Igniter installation. To do this you will put all of the directories located inside your application folder into their +own sub-folder.

+ +

For example, let's say you want to create two applications, "foo" and "bar". You will structure your +application folder like this: + +system/application/foo/
+system/application/foo/config/
+system/application/foo/controllers/
+system/application/foo/errors/
+system/application/foo/models/
+system/application/foo/scripts/
+system/application/foo/views/
+system/application/bar/
+system/application/bar/config/
+system/application/bar/controllers/
+system/application/bar/errors/
+system/application/bar/models/
+system/application/bar/scripts/
+system/application/bar/views/
+ + +

To select a particular application for use requires that you open your main index.php file and set the $application_folder +variable. For example, to select the "foo" application for use you would do this:

+ +$application_folder = "foo"; + + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/plugins.html b/user_guide/general/plugins.html new file mode 100644 index 000000000..c28dd321f --- /dev/null +++ b/user_guide/general/plugins.html @@ -0,0 +1,125 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Plugins

+ +

Plugins work almost identically to Helpers. The main difference is that a plugin usually +provides a single function, whereas a Helper is usually a collection of functions. Helpers are also considerd a part of +the core system; plugins are intended to be created and shared by our community.

+ + +

Loading a Plugin

+ +

Loading a plugin file is quite simple using the following function:

+ +$this->load->plugin('name'); + +

Where name is the file name of the plugin, without the .php file extension or the "plugin" part.

+ +

For example, to load the Captcha plugin, which is named captcha_pi.php, you will do this:

+ +$this->load->plugin('captcha'); + +

A plugin can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), +as long as you load it before you use it. You can load your plugins in your controller constructor so that they become available +automatically in any function, or you can load a plugin in a specific function that needs it.

+ +

Note: The Plugin loading function above does not return a value, so don't try to assign it to a variable. Just use it as shown.

+ + +

Loading Multiple Plugins

+ +

If you need to load more than one plugin you can specify them in an array, like this:

+ +$this->load->plugin( array('plugin1', 'plugin2', 'plugin3') ); + +

Auto-loading Plugins

+ +

If you find that you need a particular plugin globally throughout your application, you can tell Code Igniter to auto-load it +during system initialization. This is done by opening the application/config/autoload.php file and adding the plugin to the autoload array.

+ + +

Using a Plugin

+ +

Once you've loaded the Plugin, you'll call it the way you would a standard PHP function.

+ + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/quick_reference.html b/user_guide/general/quick_reference.html new file mode 100644 index 000000000..3e95fac6c --- /dev/null +++ b/user_guide/general/quick_reference.html @@ -0,0 +1,83 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Quick Reference Chart

+ +

For a PDF version of this chart, click here. + +

+ +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/requirements.html b/user_guide/general/requirements.html new file mode 100644 index 000000000..8e18ab09a --- /dev/null +++ b/user_guide/general/requirements.html @@ -0,0 +1,88 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Server Requirements

+ + + + + +
+ + + + + + + + \ No newline at end of file diff --git a/user_guide/general/routing.html b/user_guide/general/routing.html new file mode 100644 index 000000000..c6e8bd9cf --- /dev/null +++ b/user_guide/general/routing.html @@ -0,0 +1,163 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

URI Routing

+ +

Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. +The segments in a URI normally follow this pattern:

+ +www.your-site.com/class/function/id/ + +

In some instances, however, you may want to remap this relationship so that a different class/function can be called +instead of the one corresponding to the URL.

+ +

For example, lets say you want your URLs to have this prototype:

+ +

+www.your-site.com/product/1/
+www.your-site.com/product/2/
+www.your-site.com/product/3/
+www.your-site.com/product/4/ +

+ +

Normally the second segment of the URL is reserved for the function name, but in the example above, it instead has a product ID. +To overcome this, Code Igniter allows you to remap the URI handler.

+ + +

Setting your own routing rules

+ +

Routing rules are defined in your application/config/routes.php file. In it you'll see an array called $route, that +you can use to specify your own routing criteria. A typical route might look something like this:

+ +$route['product/:num'] = "catalog/product_lookup"; + +

In a route, the array key contains the URI to be matched, while the array value contains the destination it should be re-routed to. +In the above example, if the literal word "product" is found in the first segment of the URL, and a number is found in the second segment, +the "catalog" class and the "product_lookup" method are instead used.

+ +

You can match literal values or you can use two wildcard types:

+ +

+:num
+:any +

+ +

:num will match a segment containing only numbers.
+:any will match a segment containing any character. +

+ +

Note: Routes will run in the order they are defined. +Higher routes will always take precedence over lower ones.

+ + +

Examples

+ +

Here are a few routing examples:

+ +$route['journals'] = "blogs"; +

Any URL containing the word "journals" in the first segment will be remapped to the "blogs" class.

+ +$route['blog/joe'] = "blogs/users/34"; +

Any URL containing the segments blog/joe will be remapped to the "blogs" class and the "users" method. The ID will be set to "34".

+ + +$route['product/:any'] = "catalog/product_lookup"; +

Any URL with "product" as the first segment, and anything in the second will be remapped to the "catalog" class and the "product_lookup" method.

+ +

Important: Do not use leading/trailing slashes.

+ + +

Reserved Route

+ +

There are two reserved routes:

+ +$route['default_controller'] = 'welcome'; + +

This route indicates which controller class should be loaded if the URI contains no data, which will be the case +when people load your root URL. In the above example, the "welcome" class would be loaded. You +are encouraged to always have a default route otherwise a 404 page will appear by default.

+ +$route['scaffolding_trigger'] = 'scaffolding'; + +

This route lets you set a secret word, which when present in the URL, triggers the scaffolding feature. +Please read the Scaffolding page for details.

+ + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/scaffolding.html b/user_guide/general/scaffolding.html new file mode 100644 index 000000000..3b32c3692 --- /dev/null +++ b/user_guide/general/scaffolding.html @@ -0,0 +1,153 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Scaffolding

+ +

Code Igniter's Scaffolding feature provides a fast and very convenient way to add, edit, or delete information in your database +during development.

+ +

Very Important: Scaffolding is intended for development use only. It provides very little +security other than a "secret" word, so anyone who has access to your Code Igniter site can potentially edit or delete your information. +If you use scaffolding make sure you disable it immediately after you are through using it. DO NOT leave it enabled on a live site. +And please, set a secret word before you use it.

+ + +

Why would someone use scaffolding?

+ +

Here's a typical scenario: You create a new database table during development and you'd like a quick way to insert some data +into it to work with. Without scaffolding your choices are either to write some inserts using the command line or to use a +database management tool like phpMyAdmin. With Code Igniter's scaffolding feature you can quickly add some data using its browser +interface. And when you are through using the data you can easily delete it.

+ +

Setting a Secret Word

+ +

Before enabling scaffolding please take a moment to set a secret word. This word, when encountered in your URL, +will launch the scaffolding interface, so please pick something obscure that no one is likely to guess.

+ +

To set a secret word, open your application/config/routes.php file and look for this item:

+ +$route['scaffolding_trigger'] = ''; + +

Once you've found it add your own unique word.

+ +

Note: The scaffolding word can not start with an underscore.

+ + +

Enabling Scaffolding

+ +

Note: The information on this page assumes you already know how controllers work, and that you have +a working one available. It also assumes you have configured Code Igniter to auto-connect to your database. +If not, the information here won't be very relevant, so you are encouraged to go through those sections first. +Lastly, it assumes you understand what a class constructor is. If not, read the last section of the controllers +page.

+ +

To enable scaffolding you will initialize it in your constructor like this:

+ + +<?php
+class Blog extends Controller {
+
+       function Blog()
+       {
+            parent::Controller();

+            $this->load->scaffolding('table_name');
+       }
+}
+?>
+ +

Where table_name is the name of the table (table, not database) you wish to work with.

+ +

Once you've initialized scaffolding, you will access it with this URL prototype: + +www.your-site.com/index.php/class/secret_word/ + +

For example, using a controller named Blog, and abracadabra as the secret word, +you would access scaffolding like this:

+ +www.your-site.com/index.php/blog/abracadabra/ + +

The scaffolding interface should be self-explanatory. You can add, edit or delete records.

+ + +

A Final Note:

+ +

The scaffolding feature will only work with tables that contain a primary key, as this is information is needed to perform the various +database functions.

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/scripts.html b/user_guide/general/scripts.html new file mode 100644 index 000000000..d0b5d26f6 --- /dev/null +++ b/user_guide/general/scripts.html @@ -0,0 +1,115 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

The Scripts Folder

+ +

Inside the application directory you'll find a folder called scripts. Its purpose is +to give you a place to store your own script includes or classes.

+ +

Loading a Script

+ +

Your scripts can be loaded in your controllers or views using the following function:

+ +$this->load->script('name'); + +

Where name is the file name of the script, without the .php file extension.

+ +

For example, to load a file called utilities.php you would do this:

+ +$this->load->script('utilities'); + +

If you load a script in your controller constructor it will be available +automatically in any function, or you can load it in a specific function that needs it.

+ +

Note: The Script loading function above does not return a value, so don't try to assign it to a variable. Just use it as shown.

+ +

Loading Multiple Scripts

+ +

If you need to load more than one script you can specify them in an array, like this:

+ +$this->load->script( array('script1', 'script2', 'script3') ); + +

Auto-loading Scripts

+ +

If you find that you need a particular script globally throughout your application, you can tell Code Igniter to auto-load it +during system initialization. This is done by opening the application/config/autoload.php file and adding +the script to the autoload array.

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/security.html b/user_guide/general/security.html new file mode 100644 index 000000000..06287a23b --- /dev/null +++ b/user_guide/general/security.html @@ -0,0 +1,159 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Security

+ +

This page describes some "best practices" regarding web security, and details +Code Igniter's internal security features.

+ + +

URI Security

+ +

Code Igniter is fairly restrictive regarding which characters it allows in your URI strings in order to help +minimize the possibility that malicious data can be passed to your application. URIs may only contain the following: +

+ + + +

GET, POST, and COOKIE Data

+ +

GET data is simply disallowed by Code Igniter since the system utilizes URI segments rather than traditional URL query strings (unless +you have the query string option enabled in your config file). The global GET +array is unset by the Input class during system initialization.

+ +

Register_globals

+ +

During system initialization all global variables are unset, except those found in the $_POST and $_COOKIE arrays. The unsetting +routine is effectively the same as register_globals = off.

+ + +

magic_quotes_runtime

+ +

The magic_quotes_runtime directive is turned off during system initialization so that you don't have to remove slashes when +retrieving data from your database.

+ +


Best Practices

+ +

Before accepting any data into your application, whether it be POST data from a form submission, COOKIE data, URI data, +XML-RPC data, or even data from the SERVER array, you are encouraged to practice this three step approach:

+ +
    + +
  1. Filter the data as if it were tainted.
  2. +
  3. Validate the data to ensure it conforms to the correct type, length, size, etc. (sometimes this step can replace step one)
  4. +
  5. Escape the data before submitting it into your database.
  6. +
+ +Code Igniter provides the following functions to assist in this process:

+ + + + + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/urls.html b/user_guide/general/urls.html new file mode 100644 index 000000000..8daef51a6 --- /dev/null +++ b/user_guide/general/urls.html @@ -0,0 +1,159 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Code Igniter URLs

+ +

By default, URLs in Code Igniter are designed to be search-engine and human friendly. Rather than using the standard "query string" +approach to URLs that is synonymous with dynamic systems, Code Igniter uses a segment-based approach:

+ +www.your-site.com/news/article/my_article + +

Note: Query string URLs can be optionally enabled, as described below.

+ +

URI Segments

+ +

The segments in the URL, in following with the Model-View-Controller approach, usually represent:

+ +www.your-site.com/class/function/ID + +
    +
  1. The first segment represents the controller class that should be invoked.
  2. +
  3. The second segment represents the class function, or method, that should be called.
  4. +
  5. The third, and any additional segments, represent the ID and any variables that will be passed to the controller.

    +
+ +

The URI Class and the URL Helper +contain functions that make it easy to work with your URI data. In addition, your URLs can be remapped using the +URI Routing feature for more flexibility.

+ + + +

Removing the index.php file

+ +

By default, the index.php file will be included in your URLs: + +www.your-site.com/index.php/news/article/my_article + +

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example + of such a file, using the "negative" method in which everything is redirected except the specified items:

+ +RewriteEngine on
+RewriteCond $1 !^(index\.php|images|robots\.txt)
+RewriteRule ^(.*)$ /index.php/$1 [L]
+ +

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as +a request for your index.php file.

+ + +

Adding a URL Suffix

+ +

In your config/config.php file you can specify a suffix that will be added to all URLs generated +by Code Igniter. For example, if a URL is this: + +www.your-site.com/index.php/products/view/shoes + +

You can optionaally add a suffix, like .html, making the page appear to be of a certain type:

+ +www.your-site.com/index.php/products/view/shoes.html + + +

Enabling Query Strings

+ +

In some cases you might prefer to use query strings URLs:

+ +index.php?c=products&m=view&id=345 + +

Code Igniter optionally supports this capability, which can be enabled in your application/config.php file. If you +open your config file you'll see these items:

+ +$config['enable_query_strings'] = FALSE;
+$config['controller_trigger'] = 'c';
+$config['function_trigger'] = 'm';
+ +

If you change "enable_query_strings" to TRUE this feature will become active. Your controllers and functions will then +be accessible using the "trigger" words you've set to invoke your controllers and methods:

+ +index.php?c=controller&m=method + +

Please note: If you are using query strings you will have to build your own URLs, rather than utilizing +the URL helpers (and other helpers that generate URLs, like some of the form helpers) as these are designed to work with +segment based URLs.

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/general/views.html b/user_guide/general/views.html new file mode 100644 index 000000000..14b03e58f --- /dev/null +++ b/user_guide/general/views.html @@ -0,0 +1,249 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ +

Views

+ +

A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. +In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type +of hierarchy.

+ +

Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the +traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page +you should do so before continuing.

+ +

Using the example controller you created in the controller page, let's add a view to it.

+ +

Creating a View

+ +

Using your text editor, create a file called blogview.php, and put this in it:

+ + + +

Then save the file in your application/views/ folder.

+ +

Loading a View

+ +

To load a particular view file you will use the following function:

+ +$this->load->view('name'); + +

Where name is the name of your view file, without the .php file extension.

+ +

Now, open the controller file you made earlier called blog.php, and replace the echo statement with the view loading function:

+ + + + + +

If you visit the your site using the URL you did earlier you should see your new view. The URL was similar to this:

+ +www.your-site.com/index.php/blog/ + + +

Adding Dynamic Data to the View

+ +

Data is passed from the controller to the view by way of an array or an object in the second +parameter of the view loading function. Here is an example using an array:

+ +$data = array(
+               'title' => 'My Title',
+               'heading' => 'My Heading'
+               'message' => 'My Message'
+          );
+
+$this->load->view('blogview', $data);
+ +

And here's an example using an object:

+ +$data = new Someclass();
+$this->load->view('blogview', $data);
+ +

Note: If you use an object, the class variables will be turned into array elements.

+ + +

Let's try it with your controller file. Open it add this code:

+ + + + +

Now open your view file and change the text to variables that correspond to the array keys in your data:

+ + + + +

Then load the page at the URL you've been using and you should see the variables replaced.

+ +

Note: Youl'll notice that in the example above we are using PHP's alternative syntax. If you +are not familiar with it you can read about it here.

+ +

Creating Loops

+ +

The data array you pass to your view files is not limited to simple variables. You can +pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you +pull data from your database it will typically be in the form of a multi-dimensional array.

+ +

Here's a simple example. Add this to your controller:

+ + + + +

Now open your view file and create a loop:

+ + + + + + +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b