0
votes

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

I'm manually passing 39 in upper URL to know how to work with parameters.

I want to display 39 in confirm.php but it's not working. As soon, i'm passing $id in actionConfirm($id) of SiteController Controller. Page Not Found Error Coming. What can be the problem.

I just asked one question URL Routing, where i got my answer. But, now here i got stuck.

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));
    }
}

Error Coming

enter image description here

How to bring 39 from URL to confirm.php page. Please help me. Forgive me, If this is a silly question.

1
Possible duplicate of URL Routing - yii-basic-app - Yii2 - soju
replace <?echo $id;?> to ` <?php echo $id;?>`. - GAMITG
No Mr @gamitg. I Did. But, still not worked. You know. You previously solved my question. This is different question. I'm not able to get 39 in my confirm.php page. - Nana Partykar
If you remove the $id from the actionConfirm($id), is the site working then? If not, your problem is not the passing of the param, but something different. - Asped
not working after removing $id from actionConfirm, when i'm not passing any thing and no 39 in URL. confirm.php works fine Mr @Asped - Nana Partykar

1 Answers

1
votes

are you sure that you have enabled

short_open_tag=On

in your php.ini?

also the correct short code is

You can also see this discussion: PHP echo vs PHP short tags

btw you don't need both if you are using a parameter in the function name here actionConfirm($id) you will have a required parameter which is filled by the parameter set by the GET params from your request URL. Which is in your case /site/confirm/39/

So either set them in the function name

public function actionConfirm($id)

or get them manually

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

You don't need both.

But this isn't your problem. You still don't call the correct action in the correct controller.

Use this urlManager rules

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

Those 3 which you have added are wrong remove them.

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

forgot to mention that if you don't have your own rules just don't set any rules in the settings then the default rules will be used which handle most of the queries normally.