Welcome Guest, Not a member yet? Register   Sign In
First segment routing question
#1

[eluser]alexbet[/eluser]
Hi All,

I have a question about routing. I have done a blog for a client, but he wants the URLs for the posts to be like in WordPress, with first the date of the post, and then the post title. Here is the general idea on how the URL to the post should be:

http://domain.com/2011/01/01/post-title-goes-here

The way CI works, when I set the routes, I can only set the first segment to be the same as a class name. But, in the above example, the first segment of the URL is the year from the date of the post, and this means that the year will change, it will not be the same. Is there a way to add a wildcard for the first segment when you set the routing and then process what is in the first segment somehow to load the correct class?

Any help is appreciated.

Thanks!
Alex
#2

[eluser]ricpan[/eluser]
Hi Alex,

I believe you could find the answer here: http://ellislab.com/codeigniter/user-gui...uting.html

See you

Ricardo
#3

[eluser]alexbet[/eluser]
Thanks Ricardo, but all of those examples have "product" in the first segment and that doesn't change. In my example, the year of the post, 2011 in my example, would change depending on when the post was made. For example, for posts made last year, it would be 2010, the year before 2009, etc. See what I mean? I did look at that page in the manual, but couldn't find the answer I was looking for, unless I make routes for each one of the years. I hope though that there is a better solution.
#4

[eluser]theprodigy[/eluser]
Code:
$route[':num/:num/:num/(:any)'] = "blog/view/$1";
#5

[eluser]toopay[/eluser]
Use some regex in your route, for proper routes. something like

Code:
$route['^(19[0-9][0-9]|20[0-9][0-9])/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(:any)$'] = "blog/lookup/$1-$2-$3_$4";

With above routes, for example someone try to access url like : http://myblog.com/3011/0/41/hello-world will get an error, since the route regex only match with right mysql date pattern. http://myblog.com/2009/12/31/hello-world, then it will reroute to your 'blog' controller and lookup function, with date and title parameters.
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Blog extends CI_Controller {
    public function lookup($slug = '')
    {
        var_dump($slug);
    }
}
Above example controller should give you : '2009-12-31_hello-world'. The rest should easy right? ;-)
#6

[eluser]alexbet[/eluser]
Thanks a lot to both of you! I really appreciate it!

Both worked fine, so I am going to have to decide which one to use. Smile
#7

[eluser]CroNiX[/eluser]
Personally, I'd just use wordpress for blogging and integrate it with your CI app. No need to reinvent the wheel and WP is probably the best at what it does already.




Theme © iAndrew 2016 - Forum software by © MyBB