2015-10-05 60 views
1

我試圖在URL中傳遞ID。我很難做到這一點。所以,我正在瀏覽像Larry Ullman - URL Routing這樣的教程。URL路由 - yii-basic-app - Yii2

我的問題是:我創建了confirm.php頁面並創建了一個控制器,但是像NOTFOUND(#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> 

錯誤未來=>未找到(#404)

enter image description here

i之後得到這個錯誤,我添加幾個碼配置/ 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頁面。

怎麼辦?我沒有想法。請幫我解決這個問題。

+0

添加該代碼,你可以發佈確認的'位置。 php' – GAMITG

+0

yii2-app-basic/views/site/confirm.php Mr @gamitg –

+1

你在'w中添加'.htaccess'文件eb'文件夾? – GAMITG

回答

2

您必須在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 
2
'urlManager' => [ 
     'enablePrettyUrl' => true, 
     'enableStrictParsing' => false, 
     'showScriptName' => false, 
     'rules' => [ 
      '<controller>/<action:\w+>.<_format>' => '<controller>/<action>', 
      'v1/<controller>/<action:\w+>.<_format>' => 'v1/<controller>/<action>', 
     ], 
    ], 
+0

請在main.php中添加組件部分 –

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