【问题标题】:Passing Parameter To Controller - yii2-app-basic Yii2将参数传递给控制器​​ - yii2-app-basic Yii2
【发布时间】:2015-10-05 12:48:24
【问题描述】:

http://localhost/yii2-app-basic/web/site/confirm/39/

我在上层 URL 中手动传递 39 以了解如何使用参数。

我想在confirm.php 中显示 39,但它不起作用。很快,我将在actionConfirm($id)SiteController 控制器中传递$id。 页面未找到错误来了。可能是什么问题。

我刚刚问了一个问题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));
    }
}

错误来了

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

【问题讨论】:

  • &lt;?echo $id;?&gt; 替换为` `。
  • 没有 @gamitg 先生。我做到了。但是,仍然没有工作。你知道。你之前解决了我的问题。这是不同的问题。我无法在我的 confirm.php 页面中获得 39。
  • 如果您从actionConfirm($id) 中删除$id,那么该站点是否正常工作?如果没有,你的问题不是参数的传递,而是不同的东西。
  • 从 actionConfirm 中删除 $id 后不起作用,当我没有传递任何东西并且 URL 中没有 39 时。 confirm.php 工作正常 @Asped 先生

标签: php yii2 yii2-basic-app


【解决方案1】:

你确定你开启了吗

short_open_tag=On

在你的 php.ini 中?

正确的短代码也是

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

顺便说一句,如果您在此处的函数名称中使用参数 actionConfirm($id),则不需要两者,您将拥有一个必需的参数,该参数由请求 URL 中的 GET 参数设置的参数填充。 你的情况是/site/confirm/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+>' => '/',

忘了提一下,如果您没有自己的规则,只是不要在设置中设置任何规则,那么将使用默认规则来正常处理大多数查询。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多