Post Pic

CodeIgniter – Change the name of your methods in a url

Now think about these following factors:

  • Hide your original method name from the URI
  • Named second segment of your URI as you like
  • You want to make a control over your controller methods so that you can decide which method should be called using URI

Now it’s time to talk in details.

First from the above factors is hiding original method name from URI. Yes, you can do it easily by using the _remap method. Let one of your controller methods is display_successful_message which you should typically browse as:

http://website.com/blog/display_successful_message

But you want to design your URI as follows

http://website.com/blog/successful

So that it calls the method display_successful_message

Now the second factor is naming second segment of URI as you like. You know that identifiers (name of variable, function, method, constant etc.) does not allow using any character except the following rule:

^[a-zA-Z_]+[a-zA-Z0-9_]?$

So you could not use any other character rather than alpha, numeric or underscore. But you like to use the dash character ‘-’ in URI as a replacement of the underscore character ‘_’.
More practically let one of your controller methods is about_me which you should typically browse as:

http://website.com/blog/about_me

But you want to design your URI as follows

http://website.com/blog/about-me

So that about_me method is get called. Note that you can also do it by CI’s URI routing feature. But it’s really paining and using _remap it’s become easier and organized.

And the last factor. You are going to make a control over your controller methods. You like to create a method’s white list which will be allowed to call using URI. Just create the list under the _reamp method. Rest of the task will do by the _remap. As for example you have a method named secure_method and you don’t like to call the method using URI. To do so don’t include the method in your white list. You can also do it another way. Just place an underscore character as a prefix of your method name (_secure_method) so that controller ignores calling the method using URI.

Before using the _remap method you should know that _remap 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.

Many lectures have delivered already. Now it’s time to show the practical.
Look at the following script:

class Blog extends Controller
{
function _remap( $method )
{
// $method contains the second segment of your URI
>switch( $method )
{

case 'about-me':
$this->about_me();

break;

case 'successful':
$this->display_successful_message();

break;

default:
$this->page_not_found();

break;
}
}
function index()
{
// ---
}
function about_me()
{
// ---
}
function display_successful_message()
{
// ---
}
function page_not_found ()
{
// ---
}
function secure_method()
{
// ---
}
function Blog()
{
parent::Controller();
}
}

Now review the previous factors. The examples I discussed about each factor are applied on the above script. I think now it is easier to understand how to use _remap and what’s it benefits and power.

ANM_CI is a PHP class which is developed to use as a CI’s user library. In the class there is a method named remap which will make your life easier to use the CI’s _remap method.

Related posts:

  1. Setting Multiple Websites in Codeigniter Installation In this tutorial, I want to demonstrate that how to...
  2. Contact Us We’re happy to discuss any ideas We pay close attention...
  3. Important Website Security Tips Website Security is a very important subject and always needs...
  4. Benefits of Output Buffering If you are not using PHP Output Buffering, you have...

Related posts brought to you by Yet Another Related Posts Plugin.

5 Responses

Its a very useful post

I think that adding some lines to the system\application\config\routes.php file is better:

$route['(.+)-(.+)-(.+)-(.+)-(.+)'] = “$1_$2_$3_$4_$5″;
$route['(.+)-(.+)-(.+)-(.+)'] = “$1_$2_$3_$4″;
$route['(.+)-(.+)-(.+)'] = “$1_$2_$3″;
$route['(.+)-(.+)'] = “$1_$2″;

This supports up to five dash separated segments, without having to add more and more lines when you add a new function.

http://www.wakamolee.com/desarrollo/codeigniter-urls-con-guion/

12.15.09

Also you can try:

$method = str_replace( “-”, “_”, $method );
$this->$method();

12.15.09

@james: str_replace is fine, but routing is a optimal method in codeigniter to change urls

12.15.09

very nice post. it helped.

Leave Your Response

* Name, Email, Comment are Required





Lunarpages.com Web Hosting






Legal Documents – Net Lawman


Get Adobe Flash playerPlugin by wpburn.com wordpress themes