KEMBAR78
Codeigniter : Custom Routing - Manipulate Uri | PDF
CodeIgniter
    Custom Routing : Manipulate your URI




                               Abdul Malik Ikhsan
   http://slideshare.net/samsonasik/presentations
Introduction : URI vs URL
 URI : Content type is given by http
  content negotiation
  ex : http://www.w3.org/Icons/w3c_home
  ~> content type : PNG

 URL : Content type is represent by
  spesific resource in web address
  ex :
  http://www.w3.org/Icons/w3c_home.gif
Why ?
   SEO Mith 
   Hide Original action controller
   Manage which action will be called
   URI Simplicity
    ….like this ……
     http://twitter.com/samsonasik
Default Routing


  example.com/product/detail/1
  example.com/product/detail/2
  example.com/product/detail/3
How to Change ?
 Modify route in config routes
 _remap in your controller ( modify
  from action controller only )
Modify route in config routes
////////////////custom route///////////////

$route['product/(:num)'] = "product/detail/$1";
$route['product/(:num)/get'] ="product/checkout/$1";
$route['product/(:any)'] = "product/index";
$route['blog/(:any)'] = "blogs/users/$1“;
$route[‘hehehe'] = "blogs/index“;

/* End of file routes.php */
/* Location: ./application/config/routes.php */
URI Result
 Example.com/product/15
 Exampleblog.com/blog/samsonasik
_remap in controller
public function _remap($method, $params = array())
{
   if (method_exists($this, str_replace('-','_',$method) )) {
       call_user_func_array(array($this, str_replace('-','_', $method) ), $params);
   } else{
       show_404();
   }
 }
 public function show_news($id) //will be called show-news/$id
 {
   echo 'show news'.$id;
 }
 public function comment_news($id) //will be called comment-news/$id
 {
   echo 'comment news'.$id;
 }
And one more thing ….


      Remove your
  index.php from your
   URI with .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Thank You ;)
This presentation contained copyrighted material licensed
under various creative commons licenses unless otherwise
noted:

References and Further Reading
   http://www.anmsaiful.net/blog/php/codeigniter-remap-power-benefit.html
   http://en.wikipedia.org/wiki/Content_negotiation
   http://ajaxprojects.com/ajax/newsdetails.php?itemid=55
   http://www.pierobon.org/iis/url.htm
   CodeIgniter User Guide

Photos
 http://media.popularmechanics.com/images/coaster-maverick-0708.jpg

Codeigniter : Custom Routing - Manipulate Uri

  • 1.
    CodeIgniter Custom Routing : Manipulate your URI Abdul Malik Ikhsan http://slideshare.net/samsonasik/presentations
  • 2.
    Introduction : URIvs URL  URI : Content type is given by http content negotiation ex : http://www.w3.org/Icons/w3c_home ~> content type : PNG  URL : Content type is represent by spesific resource in web address ex : http://www.w3.org/Icons/w3c_home.gif
  • 3.
    Why ?  SEO Mith   Hide Original action controller  Manage which action will be called  URI Simplicity ….like this ……  http://twitter.com/samsonasik
  • 4.
    Default Routing example.com/product/detail/1  example.com/product/detail/2  example.com/product/detail/3
  • 5.
    How to Change?  Modify route in config routes  _remap in your controller ( modify from action controller only )
  • 6.
    Modify route inconfig routes ////////////////custom route/////////////// $route['product/(:num)'] = "product/detail/$1"; $route['product/(:num)/get'] ="product/checkout/$1"; $route['product/(:any)'] = "product/index"; $route['blog/(:any)'] = "blogs/users/$1“; $route[‘hehehe'] = "blogs/index“; /* End of file routes.php */ /* Location: ./application/config/routes.php */
  • 7.
    URI Result  Example.com/product/15 Exampleblog.com/blog/samsonasik
  • 8.
    _remap in controller publicfunction _remap($method, $params = array()) { if (method_exists($this, str_replace('-','_',$method) )) { call_user_func_array(array($this, str_replace('-','_', $method) ), $params); } else{ show_404(); } } public function show_news($id) //will be called show-news/$id { echo 'show news'.$id; } public function comment_news($id) //will be called comment-news/$id { echo 'comment news'.$id; }
  • 9.
    And one morething …. Remove your index.php from your URI with .htaccess
  • 10.
    RewriteEngine On RewriteCond %{REQUEST_FILENAME}-s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L]
  • 11.
  • 12.
    This presentation containedcopyrighted material licensed under various creative commons licenses unless otherwise noted: References and Further Reading  http://www.anmsaiful.net/blog/php/codeigniter-remap-power-benefit.html  http://en.wikipedia.org/wiki/Content_negotiation  http://ajaxprojects.com/ajax/newsdetails.php?itemid=55  http://www.pierobon.org/iis/url.htm  CodeIgniter User Guide Photos  http://media.popularmechanics.com/images/coaster-maverick-0708.jpg