【问题标题】:Not Found Page Working On Backend But Not Working at Frontend Yii2找不到页面在后端工作但在前端 Yii2 不工作
【发布时间】:2021-11-24 01:45:21
【问题描述】:

我在我的实时服务器上安装了 yii,我们有两个接口,一个后端,一个前端。在后端我们添加产品,在前端为产品呈现视图。问题是,例如,这是我们的网站https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie 产品页面,如果您在 url 的最后放置任何类似https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie/03003300303094949949494 的内容,该页面将保持不变,这意味着它应该显示未找到的页面,但相同的页面再次出现.

下面是我的配置文件代码前端。

<?php
$params = array_merge(
require __DIR__ . '/../../common/config/params.php',
require __DIR__ . '/../../common/config/params-local.php',
require __DIR__ . '/params.php',
require __DIR__ . '/params-local.php'
);

 return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
    'request' => [
        'csrfParam' => '_csrf-frontend',
    ],
'cache' => [
        'class' => 'yii\caching\FileCache',],
    'user' => [
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
    ],
    'session' => [
        // this is the name of the session cookie used for login on the frontend
        'name' => 'advanced-frontend',
    ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
   'errorHandler' => [
        'errorAction' => 'site/error',
    ],
   


  'urlManager' => [


  'baseUrl' => '',
  'enablePrettyUrl' => false,
  'showScriptName' => false,
  'enableStrictParsing' => true,
  'rules'=>array(
  '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            'class' => 'yii\web\UrlNormalizer',
  'collapseSlashes' => true,
  'normalizeTrailingSlash' => true,
  ),

  ],
  ],
'params' => $params,
 ];

我的站点目录中有error.php,下面是它的代码

<?php

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

use yii\helpers\Html;

$this->title = $name;
?>
<h1><?= Html::encode($this->title) ?></h1>

<div class="alert alert-danger">
    <?= nl2br(Html::encode($message)) ?>
</div>

<p>
    The above error occurred while the Web server was processing your request.
</p>
<p>
    Please contact us if you think this is a server error. Thank you.
</p>

【问题讨论】:

    标签: yii yii2 http-status-code-404


    【解决方案1】:

    很奇怪,您禁用了“enablePrettyUrl”,并使用了漂亮的 url。如果“/products/”是控制器名称并且假设“IPhone-12-Mini-Scherm-Reparatie”是一个名为“url”的参数,那么您可以像这样更改 urlManager:

    'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '/'=>'site/index',
                '/products/<id>/<url>'=>'products/view',
                '/products/<id>'=>'products/view',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
            ],
        ],
    

    在控制器中应该是这样的

    public function actionView($id, $url) {
        $product = Product::find()->where(['id'=>$id])->one();
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-07
      • 2021-11-29
      • 1970-01-01
      • 2021-10-10
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      相关资源
      最近更新 更多