Static pages again

I’ve followed the instructions on this article to display static pages:

http://www.yiiframework.com/doc/cookbook/22/

Now I have the problem, that I want to have an url which looks like this:

http://wwww.domain.com/company/team

instead of:

http://wwww.domain.com/company.team

how can I achive this? any ideas?

You may consider writing beforeAction() which converts slashes to dots in the view GET parameter.

Ok, but if I do so, I’ll get the following error:

Unable to resolve the request "company/team"

I think that Yii is looking for a controller named "company" and the action "team". How can I achive that this url is handled by the SiteController?

Currently I have the following rule:

‘<view:\w+>’ => ‘site/page’

Say I go to: index.php/site/page/view/more.info

Then this is what I see in beforeAction():

$action->view: NULL

$action->viewParam: view

$action->requestedView: more.info

But for index.php/site/page/view/more/info:

$action->view: NULL

$action->viewParam: view

$action->requestedView: more

So I can’t get a handle on the requested view. (The latter 404s looking for /more)

In the latter case, I presume Yii should be passing "more/info", and that is what we can munge.

Anyone with any ideas before I raise a bug against this?

Well, this IS confusing. The solution is to set ‘appendParams’=>false for the url manager, or add a rule saying GET parameters should be appended in the query part.

I still don’t understand. What is ‘the view GET parameter’?

Setting ‘appendParams’=>false doesn’t change the values returned by index.php/site/page/view/more.info or index.php/site/page/view/more/info the results are the same as above.

What is it that should change and where do you get the value to change it? I can’t see it.

I have no idea what you mean by ‘add a rule saying GET parameters should be appended in the query part’.

Here’s the stack trace for the 404 when calling /site/page/view/more/info with ‘appendParams’ => false:




[20:42:44.549][error][exception.CHttpException.404] exception 'CHttpException' with message

'The requested view "more" is not found.'

in /home/marc/public_html/yii/framework/web/actions/CViewAction.php:110


Stack trace: 


#0 /home/marc/public_html/yii/framework/web/actions/CViewAction.php(121): CViewAction->resolveView('more') 

#1 /home/marc/public_html/yii/framework/web/CController.php(300): CViewAction->run() 

#2 /home/marc/public_html/yii/framework/web/CController.php(278): CController->runAction(Object(CViewAction)) 

#3 /home/marc/public_html/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CViewAction), Array) 

#4 /home/marc/public_html/yii/framework/web/CWebApplication.php(332): CController->run('page') 

#5 /home/marc/public_html/yii/framework/web/CWebApplication.php(120): CWebApplication->runController('site/page/view/...') 

#6 /home/marc/public_html/yii/framework/base/CApplication.php(133): CWebApplication->processRequest() 

#7 /home/marc/public_html/blog/index.php(15): CApplication->run() 

#8 {main} REQUEST_URI=/~marc/blog/site/page/view/more/info



It doesn’t leave the framework.

Sorry, my bad. Could you try adding a rule: "site/page/<view:.*>"=>"site/page" ?

No change. Same stack trace as before. Same 404.




'urlManager'=>array(

  'urlFormat'=>'path',

  'showScriptName' => false,

  'appendParams' => false,

  'rules'=>array(

    'site/page/<view:.*>"=>"site/page',

    'tag/<tag>'=>'post/list',

    'posts'=>'post/list',

    'post/<id:\d+>'=>'post/show',

    'post/update/<id:\d+>'=>'post/update',

  ),

),



In your rule, you have double quotes mixed in single quotes.

LOL That’ll teach me to cut and paste incorrectly.

Thanks.

Progress. Page Not Found: The requested view "view/more/info" is not found.




$action->view: NULL

$action->viewParam: view

$action->requestedView: view/more/info



Btw, the above is output from SiteController::beforeAction()

Also, Page Not Found: The requested view "view/more.info" is not found. Which is good!

The page in question, is: views/site/pages/more/info.php

Yeah, in beforeAction, you can now modify the view parameter so that CViewAction can recognize it.

Better way to do is to extend CViewAction and override resolveView().

I don’t understand which parameter you mean, nor to what value. I’ve tried all the things that seem obvious to me, but I can’t find what needs to be changed and to what value.

I can’t see the Yii process here for the simple user case.

Okay, that makes sense, but how do you implement that? Where would the new extended class live? Where do you instantiate it (controler::actions())? Is resolveView() simply replacing slashes with periods? It’s kind of a weird function to be protected.

Bump!

Still trying to fix this.

Assume you want to extend CViewAction. You can place the new class in ‘components’ directory. Then in your controller’s actions() method, you just need to set ‘class’=>‘MyViewClass’.

In the new class, you need to override resolveView() (that’s why it is protected). Before you do that, you may read the code in resolveView() to get better understanding.

Okay, it all works now. Yay!

Here’s what I did.

First, follow the cookbook instructions to create static pages: How to display static pages in Yii?. Get this working.

Add the following rule to urlManager in main.php:




'rules'=>array(

    'site/page/view/<view:.*>'=>'site/page',

),



(The above presumes that you are adding your static pages to site/page, of course.)

It’s necessary to override resolveView() in CViewAction for site/page, so create your own version in /components:




class SiteView extends CViewAction {

    protected function resolveView($viewPath) {

        $viewPath = preg_replace('?/?', '.', $viewPath);

        parent::resolveView($viewPath);

    }

}



Now, tell site/page to use this class instead of CViewAction:




public function actions() {

    return array(

        'page'=>array('class'=>'SiteView'),

    );

}



That’s it. It works for nested folders too (e.g. /pages/blah/moreblah/evenmoreblah/fredview.php).

I’ve still got the problem that the ‘.’ in the view parameter doesn’t get converted to a ‘/’.

If I call the following code:

$this->createUrl(‘site/page’, array(‘view’ => ‘contact.team’);

I’ll get the following url:

http://mydomain.com/contact.team

But what I want to get is:

http://mydomain.com/contact/team

How can I achive that the dot in the view parameter is converted to a slash?

I don’t know why createUrl() returns what you are getting. I suspect that if you set-up what I do and step through, then you should be able to spot where things go wrong. I just retested my stufff on the latest Yii release, and all is working okay.

Ok, I’ve tried the above configuration and still doensn’t get the ‘.’ in the view parameter converted to a ‘/’. I don’t know what is wrong :(