1

我试图在 URL 中传递 ID。我很难做到。因此,我正在阅读诸如Larry Ullman - URL Routing 之类的教程。

我的问题是:我创建了 confirm.php 页面并创建了一个控制器,但是出现了 NOT FOUND (#404) 之类的错误。

站点控制器.php

<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\swiftmailer\Mailer;

use app\models\LoginForm;
use app\models\ContactForm;
use app\models\EntryForm;
use app\models\RegisterForm;
use app\models\LoginExecForm;
use app\models\ForgotPasswordForm;

class SiteController extends Controller
{
  .
  .
  public function actionConfirm($id)
    {
        $id = Yii::$app->request->get('id');
        return $this->render('confirm');
    }
}

配置/web.php

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
    'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'rules' => [
            '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
            'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
        ],
    ], 

确认.php

<?php

/* @var $this yii\web\View */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use yii\bootstrap\Modal;
use yii\helpers\Url;

$this->title = 'Register';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-about">
    asd
</div>

错误出现 => 未找到(#404)

在此处输入图像描述

收到此错误后,我在config/web.php中添加了一些代码

<?php

$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
    'urlManager' => [
      'showScriptName' => false,
      'enablePrettyUrl' => true,
      'rules'=>array(
        '<controller:\w+>/<id:\d+>' => '/view',
        '<controller:\w+>/<action:\w+>/<id:\d+>' => '/',
        '<controller:\w+>/<action:\w+>' => '/',
        ),
    ], 

但是,现在的问题是每个页面都重定向到 index.php 页面。

该怎么办 ?我没有主意。请帮我纠正这个问题。

4

3 回答 3

2
    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'showScriptName' => false,
        'rules' => [
            '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
            'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
        ],
    ],
于 2015-10-05T12:35:58.957 回答
2

在 main.php 中添加此代码

  'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => false,
    'showScriptName' => false,
    'rules' => [
        '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
        'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
    ],
],

并将此代码添加到 .htaccessfile

于 2015-10-05T12:39:47.867 回答
2

您必须在文件夹中添加.htaccess文件,以便从 URL 中删除。webindex.php

就像,

RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
于 2015-10-05T12:29:05.563 回答