0

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.

5
  • Possible duplicate of URL Routing - yii-basic-app - Yii2 Commented Oct 5, 2015 at 12:54
  • 1
    replace <?echo $id;?> to ` <?php echo $id;?>`. Commented Oct 5, 2015 at 12:57
  • 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. Commented Oct 5, 2015 at 13:28
  • 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. Commented Oct 5, 2015 at 13:36
  • 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 Commented Oct 5, 2015 at 13:39

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Yess. short_open_tag=on in my php.ini
if you still get the error then you have still a problem with your url routing. you should be able to render some pages. What happens when you click the about link in your menu? fix this first. then your page will render the id you have submitted.
It works fine when i click register, about etc menu Mr @Bhoft. Ok. Tell me. Is this the way which i wrote in controller to access values of URL. ??
What are the urls when you click the about link? /site/about ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.