Codeigniter : Custom Routing - Manipulate Uri

25,938 views

Published on

Codeigniter custom Route

Published in: Technology, Business
3 Comments
7 Likes
Statistics
Notes
No Downloads
Views
Total views
25,938
On SlideShare
0
From Embeds
0
Number of Embeds
43
Actions
Shares
0
Downloads
130
Comments
3
Likes
7
Embeds 0
No embeds

No notes for slide

Codeigniter : Custom Routing - Manipulate Uri

  1. 1. CodeIgniter Custom Routing : Manipulate your URI Abdul Malik Ikhsan http://slideshare.net/samsonasik/presentations
  2. 2. 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
  3. 3. Why ? SEO Mith  Hide Original action controller Manage which action will be called URI Simplicity ….like this ……  http://twitter.com/samsonasik
  4. 4. Default Routing  example.com/product/detail/1  example.com/product/detail/2  example.com/product/detail/3
  5. 5. How to Change ? Modify route in config routes _remap in your controller ( modify from action controller only )
  6. 6. 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 */
  7. 7. URI Result Example.com/product/15 Exampleblog.com/blog/samsonasik
  8. 8. _remap in controllerpublic 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; }
  9. 9. And one more thing …. Remove your index.php from your URI with .htaccess
  10. 10. RewriteEngine OnRewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [NC,L]RewriteRule ^.*$ index.php [NC,L]
  11. 11. Thank You ;)
  12. 12. This presentation contained copyrighted material licensedunder various creative commons licenses unless otherwisenoted: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 GuidePhotos http://media.popularmechanics.com/images/coaster-maverick-0708.jpg

×