CodeIgniter 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.

As noted above, the second segment of the URI typically determines which
method in the controller gets called. CodeIgniter permits you to override
this behavior through the use of the _remap() method:

publicfunction_remap(){// Some code here...}

Important

If your controller contains a method named _remap(),
it will always get called regardless of what your URI contains. It
overrides the normal behavior in which the URI determines which method
is called, allowing you to define your own method routing rules.

The overridden method call (typically the second segment of the URI) will
be passed as a parameter to the _remap() method:

Any extra segments after the method name are passed into _remap() as an
optional second parameter. This array can be used in combination with
PHP’s call_user_func_array()
to emulate CodeIgniter’s default behavior.

CodeIgniter has an output class that takes care of sending your final
rendered data to the web browser automatically. More information on this
can be found in the Views and Output Class pages. In some cases, however, you might want to
post-process the finalized data in some way and send it to the browser
yourself. CodeIgniter permits you to add a method named _output()
to your controller that will receive the finalized output data.

Important

If your controller contains a method named _output(),
it will always be called by the output class instead of
echoing the finalized data directly. The first parameter of the
method will contain the finalized output.

Here is an example:

publicfunction_output($output){echo$output;}

Note

Please note that your _output() method will receive the
data in its finalized state. Benchmark and memory usage data
will be rendered, cache files written (if you have caching
enabled), and headers will be sent (if you use that
feature) before it is handed off
to the _output() method.
To have your controller’s output cached properly, its
_output() method can use:

If you are using this feature the page execution timer and
memory usage stats might not be perfectly accurate since they
will not take into account any further processing you do.
For an alternate way to control output before any of the
final processing is done, please see the available methods
in the Output Library.

In some cases you may want certain methods hidden from public access.
In order to achieve this, simply declare the method as being private
or protected and it will not be served via a URL request. For example,
if you were to have a method like this:

privatefunction_utility(){// some code}

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

example.com/index.php/blog/_utility/

Note

Prefixing method names with an underscore will also prevent
them from being called. This is a legacy feature that is left
for backwards-compatibility.

When using this feature the first segment of your URI must
specify the folder. For example, let’s say you have a controller located
here:

application/controllers/products/Shoes.php

To call the above controller your URI will look something like this:

example.com/index.php/products/shoes/show/123

Each of your sub-directories may contain a default controller which will be
called if the URL contains only the sub-folder. Simply name your default
controller as specified in your application/config/routes.php file.

CodeIgniter also permits you to remap your URIs using its URI
Routing feature.

Since your controller classes will extend the main application
controller you must be careful not to name your methods identically to
the ones used by that class, otherwise your local functions will
override them. See Reserved Names for a full
list.

Important

You should also never have a method named identically
to its class name. If you do, and there is no __construct()
method in the same class, then your e.g. Index::index()
method will be executed as a class constructor! This is a PHP4
backwards-compatibility feature.