Codeigniter Controllers
Codeigniter Controllers are the heart of your application. It enables handling of all Http GET | POST | SERVER requests of application. This part controls the mechanism of application.
What is the controller?
A Controller is solely a category file that's named in a very means which will be related to a URI.
Consider this URI:
When a controller’s name matches the primary phase of a URI, it'll be loaded.
Example:
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Note - The file should be known as ‘Blog.php’, with a capital ‘B’.
Note - category names should begin with an uppercase letter.
<?php
class Blog extends CI_Controller {
}
Controller Methods
In the on top of example, the strategy name is index().
The “index” technique is often loaded by default if the second section of the URI is empty.
Another way to indicate your “Hello World” message would be this:
Example:
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
public function comments()
{
echo 'Look at this!';
}
}
Now load the subsequent URL to check the comment method:
Passing URI Segments to your methods
If your URI contains quite 2 segments they'll be passed to your technique as parameters.
For example, let’s say you have got a URI like this:
Your technique are going to be passed URI segments three and four (“sandals” and “123”):
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
Note - If you are using the URI Routing feature, the segments passed to your method will be the re-routed ones.
Codeigniter Default Controller
CodeIgniter is told to load a default controller once a URI isn't gifted, as are going to be the case once solely your web site root address is requested.
To declare the default controller, open your "application/config/routes.php" file and set this variable:
Where ‘blog’ is the name of the controller class you want to be used.
If you currently load your main index.php file without specifying any URI segments you’ll see your “Hello World” message by default.
Codeigniter Remapping Methods Call
Codeigniter second segment of the URI typically determines which method in the controller gets called.
CodeIgniter permits you to override this behavior through the employment of the _remap() method:
public function _remap()
{
// Some code here...
}
Note - If your controller contains a method named _remap(), it will always get called regardless of what your URI contains.
It overrides the conventional behavior that|during which|within which} the URI determines which methodology is named, allowing you to define your own method routing rules.
Any further segments once the strategy name area unit passed into _remap() as AN optional second parameter.
This array is often utilized in combination with PHP’s call_user_func_array() to emulate CodeIgniter’s default behavior.
Example:
public function _remap($method, $params = array())
{
$method = 'process_'.$method;
if (method_exists($this, $method))
{
return call_user_func_array(array($this, $method), $params);
}
show_404();
}
Codeigniter Passing Output
CodeIgniter has an output category that takes care of causing your final rendered information to the net browser mechanically.
More data on this could be found within the Views and Output category pages.
In some cases, however, you would possibly need to post-process the finalized information in a way and send it to the browser yourself.
CodeIgniter permits you to feature a way named _output() to your controller that may receive the finalized output information.
Note - 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 strategy can contain the finalized output.
Example:
public function _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 are 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() technique will 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 thanks to management output before any of the ultimate process is completed, please see the available methods in the Output Library.
Codeigniter Private Methods
In some cases, you would like certain methods hidden from public access.
In order to realize this, merely declare the strategy as being non-public or protected and it'll not be served via a URL request.
For example, if you were to have a method like this:
private function _utility()
{
// some code
}