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:
- 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’.
- 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.
- In config/main.php under UrlManager create following entry:
'/article/<view>' => 'site/articles'