Skip to main content

Codeigniter php frameworks: controllers part 4

Codeigniter php frameworks will be go to the part 4 of controllers. If you have not read the previous part. Now we learn about defining a default controller. 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 controller class you want to be use. If you now load your main index.php file without specifying any URI segments you’ll see your Hello World message by default. Codeigniter php frameworks is finish talking about default controller, now we going to remapping function calls. As defining a default controller, the second segment of the URI typically determines which function in the controller get called. Codeigniter permits you to override this behavior through the use of the  function like this.
public function _remap()
{
    // Some code here...
}
The override function call (typically the second segment of the URI) will be passed as a parameter to the _remap() function like list code number 1 on this paragraph bellow. Any extra segment 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. You can see the example code at list code number 2 on this paragraph bellow. Okay enough for part 4 of controller, see you at part 5 of controller only on Codeigniter php frameworks.
Code number 1
public function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}
Code number 2
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();
}
For the note of remapping function calls , if your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.
This post of Codeigniter php frameworks: controllers part 4 is references from http://codeigniter.com/user_guide/general/controllers.html.
codeIgniter

Comments

Popular posts from this blog

Basic naming idea in codeigniter

File Naming Class files must be named in a Ucfirst-like manner, while any other file name (configurations, views, generic scripts, etc.) should be in all lowercase. INCORRECT: somelibrary.php someLibrary.php SOMELIBRARY.php Some_Library.php Application_config.php Application_Config.php applicationConfig.php CORRECT : Somelibrary.php Some_library.php applicationconfig.php application_config.php Furthermore, class file names should match the name of the class itself. For example, if you have a class named  Myclass , then its filename must be Myclass.php. Class and Method Naming Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. INCORRECT : class superclass class SuperClass CORRECT : class Super_class class Super_class {           public function __construct ()        ...

CodeIgniter: Getting Started With a Simple Example

CodeIgniter: Getting Started With a Simple Example Introduction CodeIgniter is a powerful PHP framework that can help you greatly speed up the development of your web applications. It is has a small performance footprint due to the modular approach to loading its libraries and does a great job separating logic from presentation by using a Model-View-Controller (MVC) dynamic. In this tutorial, you will learn how to create a very simple application that displays content from the database. It is not meant to provide the solution to building your own CMS, but rather to illustrate how CodeIgniter can be used. Additionally, it seeks to put in practice some of the lessons learned in the previous article and assumes you already have CodeIgniter installed on your development environment and have configured it as described in the first tutorial. The Plan In this article, you will see how to use some of CodeIgniter’s classes and functions to query a database table c...

How to setup CodeIgniter in your localhost with WAMP server

I've been developing a couple of website using codeIgniter framework. I only learned about this framework early this year 2011. Basically, I've tried installing codeIgniter from localhost and on the host server itself. How to make codeigniter work is very simple and easy. Below are the steps to kick start your first website using codeigniter. 1. Installing WAMP First and foremost, before you can have your website running on your localhost you should get a copy of WAMP(Windows Apache MySQL and PHP). It is where you can manage your server settings and Apache/MySQL services. So start downloading Wampserver now! As you can see there are 2 buttons to download between 32bits and 64bits that would be basing on your system type. To check what OS type is running click Windows logo on your keyboard + Pause/Break and a window will appear. After download, double click the file and follow the instructions given. Everything will be installed automatically  with the ...