URL路由 - yii-basic-app - Yii2

时间:2015-10-05 11:24:09

标签: php routing yii2 yii2-basic-app

我试图在URL中传递ID。我很难做到这一点。所以,我正在学习像Larry Ullman - URL Routing这样的教程。

我的问题是:我创建了confirm.php页面并创建了一个控制器,但错误就像找不到(#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');
    }
}

配置/ 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>
  

错误来了=&gt;找不到(#404)

enter image description here

我收到此错误后,在 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页面。

怎么办?我没有主意。请帮我解决这个问题。

3 个答案:

答案 0 :(得分:2)

您必须在.htaccess文件夹中添加web文件,才能从网址中删除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

答案 1 :(得分:2)

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

答案 2 :(得分: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

中添加此代码