问题
I can't show static pages. Try do it as described in doc here - http://stuff.cebe.cc/yii2-guide.pdf (on page 100) but when I enable prettyurl, it doesn't work.
Added in urlManager rules:
'urlManager' => array(
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => array(
'' => 'site/index',
'login' => 'site/login',
'contacts' => 'site/contact',
'<view:(break)>'=>'/site/page?&view=<view>',
),
),
then in SiteController added:
public function actions()
{
return [
...
'page' => [
'class'=>'yii\web\ViewAction',
],
];
}
And then created views/site/pages/break.php
<h1>View static page Break</h1>
But I get an error: Not Found (#404) Unable to resolve the request: site/page?&view=break
If i disable prettyUrl:
//'enablePrettyUrl'=>true
then i can see my page typing url: index.php?r=site/page&view=break
What's wrong with ViewAction?
回答1:
I think you are doing the rules part of your url manage wrong. Try this
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
The rules part should be important..
回答2:
I solved my problem. use such lines:
'<view:(break)>' => 'site/page',
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
I force use specific name of page for view, in my case it "break", because can't use this
'<view:[a-zA-Z0-9-]+>' => 'site/page',
(it causes crashing other rules.) I think it could better create "own rule class" extending UrlRule, but think that now I don't need this.
回答3:
I had tried this way (without rules specification) :
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => 'false'
],
回答4:
Solution is simple:
web.php code is like this
'rules' => [
'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',In SiteController don't use function actions(), instead:
public function actionIndex ($view)
{
return $this->render('/site/pages/' . $view);
} catch (InvalidParamException $e) {
throw new HttpException(404);
}
.
- If view contacts.php exists in views/site/pages/ , url is yourdomain/basic/web/site/page/contact
4.Thanks to samdark and it's article https://github.com/yiisoft/yii2/issues/2932
来源:https://stackoverflow.com/questions/28644763/yii-2-static-pages