2015-10-05 75 views
-1

http://localhost/yii2-app-basic/web/site/confirm/39/传递参数控制器 - yii2-APP-基本Yii2

我手动传递39上URL知道如何与参数工作。

我想在confirm.php中显示39,但它不起作用。尽快,我通过$idSiteController控制器。 未找到页面错误。可能是什么问题。

我刚才问了一个问题URL Routing,在那里我得到了我的答案。但是,现在我陷入了困境。

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 = 'Contact'; 
    $this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="site-about"> 
    <?echo $id;?> 
</div> 

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",array("id"=>$id)); 
    } 
} 

错误即将

enter image description here

如何从网址带来39到confirm.php页面。请帮帮我。原谅我,如果这是一个愚蠢的问题。

+0

[URL Routing-yii-basic-app-Yii2]可能的重复项目(http://stackoverflow.com/questions/32947396/url-routing-yii-basic-app-yii2) – soju

+1

将''替换为' <?php echo $ id;?>'。 – GAMITG

+0

没有Mr @gamitg。我做过了。但是,仍然没有奏效。你懂。你以前解决了我的问题。这是不同的问题。我无法在我的confirm.php页面中获得39。 –

回答

1

你确定你已经在你的php.ini启用

short_open_tag=On 

也是正确的短代码是

你也可以看到这样的讨论: PHP echo vs PHP short tags

顺便说一句,你不需要两个,如果你在函数名在此actionConfirm($ id)的使用参数您将拥有一个必需的参数,该参数由来自请求URL的GET参数设置的参数填充。 这是你的情况/网站/确认/ 39/

因此,要么将它们设置在函数名

public function actionConfirm($id) 

或手动让他们

$id = Yii::$app->request->get('id'); 

你不需要两者。

但这不是你的问题。您仍然不会在正确的控制器中调用正确的操作。

使用此urlManager规则

'<controller:\w+>/<id:\d+>' => '<controller>/view', 
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>', 

这3个已添加错了删除它们。

'<controller:\w+>/<id:\d+>' => '/view', 
    '<controller:\w+>/<action:\w+>/<id:\d+>' => '/', 
    '<controller:\w+>/<action:\w+>' => '/', 

忘了提,如果你没有自己的规则,只是没有设置在设置任何规则,那么默认规则将被用来处理大部分查询的正常进行。

+0

Yess。 short_open_tag = on在我的php.ini –

+0

如果你仍然有错误,那么你的网址路由仍然有问题。你应该能够渲染一些页面。当您点击菜单中的关于链接时会发生什么?先解决这个问题。那么你的页面将呈现你提交的ID。 – BHoft

+0

它工作正常,当我点击注册,关于等菜单Mr @Boft。好。告诉我。这是我写在控制器访问URL的值的方式。 ?? –