简体   繁体   English

Yii 2静态页面

[英]Yii 2 static pages

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. 尝试做它作为DOC这里所描述- http://stuff.cebe.cc/yii2-guide.pdf但是当我使prettyurl,它不工作(第100页上)。

Added in urlManager rules: 在urlManager规则中添加:

'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: 然后在SiteController中添加:

public function actions()
    {
        return [
            ...
            'page' => [
                'class'=>'yii\web\ViewAction',
            ],
        ];
    }

And then created views/site/pages/break.php 然后创建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 但我收到一个错误: 未找到(#404)无法解决该请求:site / page?&view = break

If i disable prettyUrl: 如果我禁用prettyUrl:

//'enablePrettyUrl'=>true

then i can see my page typing url: index.php?r=site/page&view=break 然后我可以看到我的页面键入url:index.php?r = site / page&view = break

What's wrong with ViewAction? ViewAction有什么问题?

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.. 规则部分应该很重要。

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. (这会导致其他规则崩溃。)我认为可以更好地创建扩展UrlRule的“自己的规则类”,但是现在我不需要了。

I had tried this way (without rules specification) : 我已经尝试过这种方式(没有规则规范):

        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
    ],

Solution is simple: 解决方法很简单:

  1. web.php code is like this 'rules' => [
    'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',
    web.php代码就像这样的'rules' => [
    'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',
    'rules' => [
    'site/page/<view:[a-zA-Z0-9-]+>' => 'site/index',

  2. In SiteController don't use function actions(), instead: 在SiteController中,不要使用action()函数,而是:

public function actionIndex ($view) { return $this->render('/site/pages/' . $view); } catch (InvalidParamException $e) { throw new HttpException(404); } public function actionIndex ($view) { return $this->render('/site/pages/' . $view); } catch (InvalidParamException $e) { throw new HttpException(404); } . public function actionIndex ($view) { return $this->render('/site/pages/' . $view); } catch (InvalidParamException $e) { throw new HttpException(404); }

  1. If view contacts.php exists in views/site/pages/ , url is yourdomain/basic/web/site/page/contact 如果views / site / pages /中存在视图contacts.php,则URL为yourdomain / basic / web / site / page / contact

4.Thanks to samdark and it's article https://github.com/yiisoft/yii2/issues/2932 4,感谢samdark及其文章https://github.com/yiisoft/yii2/issues/2932

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM