Yii Framework Forum: How Do I Add Date (Year/month) Of Blog Post To Url? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How Do I Add Date (Year/month) Of Blog Post To Url? Rate Topic: -----

#1 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 16 November 2012 - 08:56 PM

So far I've got slugs working in my urls, so I can view a single blog post like:

example.com/posts/here-is-an-example-title


Now I want to add the year and month of the blog post to the url. E.g.,

example.com/posts/2012/11/here-is-an-example-title


How do I do this? Additionally how would I have example.com/posts/2012/11 be an index for all posts in that month?
0

#2 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 360
  • Joined: 23-January 11

Posted 18 November 2012 - 01:32 AM

You need to tweak the already existing url manager rules in config/main.php to something like:
'/posts/<year:\d+>/<month:\d+>/<title_slug:.+>' => 'moduleId/controllerId/actionId',



Or whatever suits your requirements... . The above enforces year and month numeric parameters, as well a slug.
Therapeutic PHP sessions My LinkedIn Profile
1

#3 User is offline   CodeButterfly 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 51
  • Joined: 30-August 12

Posted 18 November 2012 - 04:45 AM

Does my action need to accept year and month as parameters? Currently my action only accepts the slug as a parameter because the slug is unique. Or do I have to accept them and just ignore them?
0

#4 User is offline   KonApaz 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,311
  • Joined: 21-February 11
  • Location:Greece

Posted 18 November 2012 - 05:07 AM

 CodeButterfly, on 18 November 2012 - 04:45 AM, said:

Does my action need to accept year and month as parameters? Currently my action only accepts the slug as a parameter because the slug is unique. Or do I have to accept them and just ignore them?


Hi CodeButterfly

for slug also see below
http://www.yiiframew...__1#entry180261


If you want the date or time for SEO reasons then you need to modify your action like actionView($year,$month,$title_slug) {...}
Yii is the best php framework in the world!
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#5 User is offline   Boaz 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 360
  • Joined: 23-January 11

Posted 18 November 2012 - 05:54 AM

 CodeButterfly, on 18 November 2012 - 04:45 AM, said:

Does my action need to accept year and month as parameters? Currently my action only accepts the slug as a parameter because the slug is unique. Or do I have to accept them and just ignore them?




Nope. You do not have to declare such variables but AFAIK they are passed to the action method. Just to complete the picture, you can declare them in the action method with "default values" thus enabling usage of URL with empty vars in relevant locations.
Therapeutic PHP sessions My LinkedIn Profile
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users