Welcome Guest, Not a member yet? Register   Sign In
Dynamic routing names
#1

(This post was last modified: 10-21-2015, 11:37 PM by ignitedcms.)

Not sure how to best explain this but the best example I can think of is wordpress.

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.


For example my default blog render calls the controller:

Code:
render_blog_page/section_id/entry_id

but I would like it read the viewfile name eg blog/blog-title.

I hope that made sense. Looking forward to your suggestions.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#2

(This post was last modified: 10-21-2015, 11:43 PM by AidanW.)

I would often load the DB class in the routes file and do something like this

PHP Code:
require_once( BASEPATH .'database/DB'EXT );
$db =& DB();

$uri explode('?'$_SERVER['REQUEST_URI']); //We don't want to include querystring data
$uri explode('/'$uri[0]);

if(
$uri[1]) //Not the homepage then check slugs
{
 if(
$uri[1] == 'blog' && $uri[2])
 {
 
//Check blog slugs
 
$db->select('post_id, post_slug');
 
$db->where('post_slug'$uri[2]);
 
$db->where('post_live'1);
 
 if(
$post $db->get('tbl_blog_post')->row_array())
 {
 
$route['blog/'.$post['post_slug']] = 'blog/post/'.$post['post_id'];
 }
 }

Reply
#3

That is a really clever way to use the routes file, I have not tried that before. In fact have hardly ever touched the routes file.

When I had to do a shop and wanted friendly SEO urls I added a column in the db for url name and my contoller would url encode the product short name automatically to populate the column. The controller would take the url 'friendly' segment and try to decode it into a product id, by comparing to the column in the table.

The problem I found then was that product names were not all unique, leading to wrong products displayed on links where they were repeated 'url names'. I did not want to force the product name to be unique so I ended up adding stuff to make it unique on purpose, like teak-chair-247.

Then I discovered that some product names were becoming just messy horrible things, so I started letting the user input an SEO friendly name, and forced that to be unique, auto filling it with a suggestion from the product name +stuff if needed, to make the suggestion unique.

It was a real pain to be honest.

However, that was some time ago. Recently I read an article (that I think was found following a link from the forum here) about not using ID's in certain cases, so am about to try to implement something similar in an existing app. I have not decided how I am going to do it yet but AidanW's answer above has given me some new ideas to test.

So although not for seo reasons, changing url's for seo or for hiding id's is a similar thing. Would be interested if anyone has any better ways to do it.

Best wishes,

Paul


PS The blog post was here: https://philsturgeon.uk/http/2015/09/03/...struction/
Reply
#4

Very interesting guys, I'd also like to add the following reference as well..

http://stackoverflow.com/questions/17042...gniter-2-x

I never thought about adding the db library to the route file like that. Something I will surely investigate today.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#5

Cheers for the link, I wouldn't worry too much about caching the routes, one extra DB query isn't going to make much difference for most apps.

Having a dedicated routes table would defo be an option.

Sometimes I would add extra routes like so, just a quick dirty method that limits the number of queries needed to one.

PHP Code:
if($uri[1]) //Not the homepage then check slugs
{
    if(
$uri[1] == 'blog' && $uri[2] == 'category' && $uri[3])
    {
        
//Check blog category slugs
        
$db->select('cat_id, cat_slug');
        
$db->where('cat_slug'$uri[3]);
        
        if(
$category $db->get('tbl_blog_category')->row_array())
        {
            
$route['blog/category/'.$category['cat_slug']] = 'blog/category/'.$category['cat_id'];
        }
    }
    elseif(
$uri[1] == 'blog' && $uri[2])
    {
        
//Check blog category slugs
        
$db->select('post_id, post_slug');
        
$db->where('post_slug'$uri[2]);
        
$db->where('post_live'1);
        
        if(
$post $db->get('tbl_blog_post')->row_array())
        {
            
$route['blog/'.$post['post_slug']] = 'blog/post/'.$post['post_id'];
        }
    }
    else
    {
        
//Some first level route
        
$db->select('sho_id, sho_slug');
        
$db->where('sho_slug'$uri[1]);
        
$db->where('sho_live'1);
        
        if(
$show $db->get('tbl_show')->row_array())
        {
            
$route[$show['sho_slug']] = 'shows/view/'.$show['sho_id'];
        }
    }

Reply
#6

(This post was last modified: 10-22-2015, 02:18 AM by John_Betong.)

@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.
Reply
#7

Thank you very much guys, I spent the most of yesterday evening exploring routing.

I must admit the way I was thinking about it was backwards.

I thought you set a rule to dynamically remap your controller to another url.

For example if you had my_controller/my_method/id in a view file I thought you could dynamically change the url displayed in the browser to blog/1.

Once I realised it works in the opposite way, i.e if you have blog/1 in your view file which then routes to the controller. I got it to work.

But then I thought, wow, this is really rather useless lol. To provide any real functionality, I would have to dynamically store my routes in a database everytime I create a new page or section. But yeah, I'm going to put this on the back burner for the time being and carefully plan how all my CMS pages will look.

Thread closed unless anyone has something to add.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#8

Hi Guys,

Please check below url.
https://osvaldas.info/smart-database-dri...odeigniter
Reply




Theme © iAndrew 2016 - Forum software by © MyBB