Static Pages Under Subdirectories

Recently I came into a situation where I have to create static pages under different subdirectory. I was actually developing a site where user post articles by creating static pages instead of dynamic page. We want the url to look like mysite.com/articles/myarticle.html instead of default like mysite.com/articles.myarticle.html. The directory structure was created like ‘site/pages/articles’ and all the articles pages were under articles subdirectory. After experimenting a lot with this issue, I finally get to the Yii IRC channel and got some help with user nicknamed ‘phpnode’ there. After working togetherly for some hours and experimenting on it, we came to the following solution:

  1. In controllers/SiteController.php file:

In function action() after the line:


'page'=>array(

	'class'=>'CViewAction',

),

add the following line:




'articles' => array('class' => 'CViewAction', 'basePath' => 'pages/articles')



here according to Yii documentation, basePath defaults to ‘pages’, so as we have created subdirectory ‘articles’ under ‘pages’ we should use ‘pages/articles’.

  1. Creating link on page:

Use the following code to create link for that article page:


Yii::app()->createUrl('site/articles', array('view' => 'myarticlepage'))

replace ‘myarticlepage’ with your page name under articles directory.

  1. In config/main.php under UrlManager create following entry:

'/article/<view>' => 'site/articles'

That’s all. :)

Regards,

Zishan J.

Hi zishanj,

Thanks for sharing your solution. I almost went mad ecause in the urlManager was miss the first slash (/), i could’t figure out why it was not working. Pretty dumb.


'web-design/<view:[\w\-]+>'=>'site/view/web-design',

instead of


'/web-design/<view:[\w\-]+>'=>'site/web-design',

Pretty sweet!!! You solution fixed it & my site works - check it out action