URL 路由 - yii-basic-app - Yii2

URL Routing - yii-basic-app - Yii2

我试图在 URL 中传递 ID。我越来越难以做到这一点。所以,我正在学习像 Larry Ullman - URL Routing.

这样的教程

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

SiteController.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');
    }
}

config/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>',
        ],
    ], 

confirm.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>

Error Coming => Not Found(#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 页面。

怎么办?我不知道。请帮我解决这个问题。

您必须在 web 文件夹中添加 .htaccess 文件,以便从 URL.

中删除 index.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
    'urlManager' => [
        'enablePrettyUrl' => true,
        'enableStrictParsing' => false,
        'showScriptName' => false,
        'rules' => [
            '<controller>/<action:\w+>.<_format>' => '<controller>/<action>',
            'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>',
        ],
    ],

在 main.php

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

并在 .htaccess 文件中添加此代码