bitmatix
(Bitmatix)
July 24, 2009, 10:49am
1
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?
qiang
(Qiang Xue)
July 25, 2009, 11:32am
2
You may consider writing beforeAction() which converts slashes to dots in the view GET parameter.
bitmatix
(Bitmatix)
July 27, 2009, 6:43am
3
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’
auxbuss
(Apps)
July 27, 2009, 10:57am
4
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.
auxbuss
(Apps)
July 28, 2009, 7:04pm
5
Anyone with any ideas before I raise a bug against this?
qiang
(Qiang Xue)
July 29, 2009, 9:32pm
6
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.
auxbuss
(Apps)
July 30, 2009, 5:51pm
7
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’.
auxbuss
(Apps)
July 30, 2009, 6:20pm
8
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.
qiang
(Qiang Xue)
July 30, 2009, 6:26pm
9
Sorry, my bad. Could you try adding a rule: "site/page/<view:.*>"=>"site/page" ?
auxbuss
(Apps)
July 30, 2009, 6:58pm
10
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',
),
),
qiang
(Qiang Xue)
July 30, 2009, 7:07pm
11
In your rule, you have double quotes mixed in single quotes.
auxbuss
(Apps)
July 30, 2009, 7:42pm
12
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
qiang
(Qiang Xue)
July 30, 2009, 8:15pm
13
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().
auxbuss
(Apps)
August 1, 2009, 4:40pm
14
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.
auxbuss
(Apps)
August 8, 2009, 8:47pm
15
auxbuss:
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.
qiang
(Qiang Xue)
August 9, 2009, 3:02am
16
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.
auxbuss
(Apps)
August 17, 2009, 10:36am
17
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).
bitmatix
(Bitmatix)
September 8, 2009, 9:30am
18
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?
auxbuss
(Apps)
September 8, 2009, 7:13pm
19
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.
bitmatix
(Bitmatix)
September 9, 2009, 8:14am
20
auxbuss:
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