Again static page URL

Well,

I can’t get this done.

With:


'<view:\w+>' => 'site/page'

as urlManager rule I can traffic requests like http:||myWebsite|about into protected/site/pages/about.php view.

but actually I want to traffic requests like http:||myWebsite|site|about into protected/site/pages/about.php view.

Can anybody help me to get the right pattern???

Thanks ahead!

(please notice I use pipe symbol instead of slash in URL addresses because of this forum prohibitive rules)


'site/<view:\w+>' => 'site/page'

Hmm… not realy:

[color="#0000FF"]Error: 404

The system is unable to find the requested action "about".[/color]

Ouups - I edited this post as I found the following!

This one is working for static pages es expected but dynamic ones are gone:


'rules'=>array(

'site/<view:\w+>' => 'site/page',

'<controller:\w+>/<id:\d+>'=>'<controller>/view',

'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

)

This one is working for dynamic pages but static are gone:


'rules'=>array(

'<controller:\w+>/<id:\d+>'=>'<controller>/view',

'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

'site/<view:\w+>' => 'site/page',

)

Any idea what to do???

Just to be clear: There is a SiteController with an actionPage($view)?

The clear answer is:

there is a SiteController with:


public function actionPage($view){

   $this->render($view);

}

but it seems this function is never called actually :frowning:

Dear Ziggi,

This is exactly the problem, I have faced many times.

If we make one rule to work, the other rule gets violated.

In your case, in order to achieve the desired result, kindly check the following.

1.Create a folder ‘about’ inside the pages folder.Create index.php inside the about directory.

2.Make the following changes inside the method actions in SiteController.php.

Remove the following:




'page'=>array(

		'class'=>'CViewAction',

			),



Add the following





'about'=>array(

		'class'=>'CViewAction',

		'basePath'=>'pages/about'

			),



Add the follwoing rule in UrlManager:




'site/<action:\w+>/<view:\w+>'=>'site/<action>',



Now you can access the contents of index.php by calling "http://localhost/blog/index.php/site/about".

site/login and site/contact are also working.

You can also put some other static pages inside the about folder.

You can call it "http://localhost/blog/index.php/site/about/otherPage"

Hi Seenivasan,

Hehe - I am a newbee in Yii framework but your way of problem solving has amused me :wink:

The solution you have provided is very, very limited. Through changing the basePath of CViewAction method you only pretend solving a problem - you still remain limited to ONE directory and one ‘index.php’ file within it.

This is nonsense.

I have already found one possible solution actually. I am not that happy with it but at least it is generic:

You keep this traffic rules:


'rules'=>array(		

   '<controller:\w+>/<id:\d+>'=>'<controller>/view',

   '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

   '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

   'site/<view:\w+>' => 'site/page',

)

Then you need to modify protected/components/Controller.php file adding a following function to it:


public function missingAction($actionID) {

   $views = $this->getViewPath();

   $staticPath = 'pages';

   if(file_exists($views . "/$staticPath/" . $actionID . ".php")) {

      $this->render("$staticPath/" . $actionID);

   } else {

      throw new CHttpException(404, Yii::t('yii', 'The system is unable to find the requested action "{action}".', array('{action}' => $actionID == '' ? $this->defaultAction : $actionID)));

   }

}

This way you can call all views within ‘site/pages’ directory by their names and they are normally rendered.

I am a genius, I know :slight_smile:

But I would be even more happy if I were able to set the right value of ‘$staticPath’ through reading the ‘basePath’ property of ‘CViewAction’ method in runtime :-)))

Regards!

Dear Friend,

I am just sharing my experience.Nothing more than that.

Moreover I can declare any number of CView actions and any number of static pages.I can

also classify the pages into different folders.

This is evident in the following code.




public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			// page action renders "static" pages stored under 'protected/views/site/pages'

			// They can be accessed via: index.php?r=site/page&view=FileName

			'about'=>array(

				'class'=>'CViewAction',

				'basePath'=>'pages/about'

			),

			'company'=>array(

				'class'=>'CViewAction',

				'basePath'=>'pages/company'

			),

			'rent'=>array(

				'class'=>'CViewAction',

				'basePath'=>'pages/landlord/rent'

			),

			'buy'=>array(

				'class'=>'CViewAction',

				'basePath'=>'pages/landlord/buy'

			),

		);

	}



Yeap - you’re right. This way you can - provided you keep manually adding code you your SiteController with each and every static page added. Nasty solution IMHO.

Regards and thank you anyway!