未找到页面在后端工作但在前端不工作 Yii2

问题描述 投票:0回答:1

我在我的实时服务器上安装了 yii,我们有两个接口,一个后端,一个前端。在后端我们添加产品,并在前端呈现产品的视图。问题是,例如,这是我们的网站 https://mmstore.be/products/714/IPhone-12-Mini-Scherm-Reparatie 产品页面,如果您在网址的最后放置任何内容,如下所示 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>
php yii yii2 http-status-code-404
1个回答
1
投票

奇怪的是,您禁用了“enablePrettyUrl”并使用漂亮的网址。如果“/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();
    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.