Dynamic routing names |
@iamthwee
>>> In wordpress when you create a blog entry it's url is by default blogid/blogdate or something. >>> But you can go into the back and change it so it show blogname/blogdate which is a more SEO friendly url. >>> So my question is can I do something like this in codeigniter without tampering with the .htaccess file. The way I attack this problem is to use the folowing: 1. Helper -> url_title() 2. Uri Class - $this->uri->_segments(1, 'Default to cannot find blog'); Minimum Table structure: table->blog title as varchar(255) slug as varchar(255) blurb as text Save each and every blog to blog table and set the following: $title = 'blog title goes here' $slug = url_title($title) // requires Url Helper to be loaded $blurb = 'Blog blurb goes here' Adjust the routes.php Routes file: ./config/routes.php $route["default_controller"] = "c_home"; $route["home"] = "c_home/index"; $route['blog'] = "c_blog/index"; $route["about"] = "c_about/index/$1"; $route["terms"] = "c_about/terms"; // fall through to getSlug $route["(.+)"] = "c_blog/getSlug/$1"; Controllers file: ./controllers/c_blog,php //======================================== class C_blog extends MY_Library { //================= function getSlug() { $slug = $this->uri->segment(1); // search blog table $ok = $this->db->blog->getSlug($slug); if( $ok ) { // populate $data['...'] = [ ['title' => '$ok->title'], ['blurb' => $ok->blurb] ]; }else{ $data['title'] == 'Unable to find blog: ' .$slug; } // call $this->view('v_blogs', $data); }// end func }// end class Please note that my function getSlug() has numerous other checks and is over a hundred lines. |
Messages In This Thread |
Dynamic routing names - by ignitedcms - 10-21-2015, 03:14 PM
RE: Dynamic routing names - by AidanW - 10-21-2015, 03:28 PM
RE: Dynamic routing names - by PaulD - 10-21-2015, 05:56 PM
RE: Dynamic routing names - by ignitedcms - 10-21-2015, 11:12 PM
RE: Dynamic routing names - by AidanW - 10-22-2015, 12:06 AM
RE: Dynamic routing names - by John_Betong - 10-22-2015, 02:13 AM
RE: Dynamic routing names - by ignitedcms - 10-23-2015, 10:57 AM
RE: Dynamic routing names - by puneetkumar - 07-13-2018, 12:27 AM
|