Gulzar Ali
Gulzar Ali

Reputation: 21

unable to render view from controller in yii

I am trying to render view from controller after moving my site to new server but it does not work here. when I display something in controller without rendering view then it works. mean I can access controller method. it is working fine at my old domain. I am using yii with wordpress and my yii project is in sub-directory. main layout is rendered successfully but not the view in views/site.


        return $this->render('index', [
            'names' => $names,
            'phones' => $phones

        ]);

and this is my view code

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\grid\GridView;
use yii\helpers\Url;
use app\models\StopTimes;
use app\controllers\SiteController;
yii::$app->assetManager->forceCopy = true;
$isActiveHide = $_GET['debug'] == 'yes' ? false: true ;
$debugMode = $_GET['debug'] == 'yes' ? true: false ;

echo "it is here";

if I try to echo $names or $phones then it show data accurately.

where I am wrong. here it shows "an internal server error have occurred" is there a way to get what the exact error is, instead of general error?

Upvotes: 0

Views: 921

Answers (2)

janavi
janavi

Reputation: 26

if you want to render view.php of your Controller then,

return $this->render('view', [
        'names' => $names,
        'phones' => $phones

    ]);

Also, whatever parameter you pass in return that will be accessible on the view file

Upvotes: 0

Addi
Addi

Reputation: 199

To answer your initial question 'Is there a way to get what the exact error is, instead of general error?'

Debug Tools: 1. Echo's - What are the contents of your Controller? How are you trying to debug from your controller? More specifically double check that you are not using echo's in Controllers because this was acceptable with older versions of yii2 but not with later versions and using echo's in controllers can lead to internal server errors. Yii2 issue 16014 Preferably use something like:

    Yii::$app->session['myvariable']  = $names;

Instead of:

    echo $names;

from your controller which you can view in your debug toolbar under Status...Request...Session. Upgrade your Yii version in your composer.json and run composer update so that the servers php version is compatible with a later version of yii2.

  1. var_dumps($names) will be useful if you can access your view.
  2. When you can access your view. Press F12. Using the Dom Explorer you will be able to isolate specific sections.
  3. Pay attention to what format your values are in that you view after pressing F12. Multiple values in curly brackets are Json and will have to be decoded into a php array and then further isolated until the actual variable is extracted eg.

        $translatedId = Yii::$app->request->post('editableKey');
        $json = Json::decode($translatedId, true);
        $id = $json['id']; 
    

    Here the editableKey is in Json format when viewed with F12. We use Json to decode and further code to isolate the $id value.

Internal Server Errors often arise when a Json value has not been decoded as in the above example.

  1. Upgrade your Yii2. Here is a more advanced template you might find useful:

       "minimum-stability": "stable",
        "require": {
            "php": ">=7.4.1",
            "yiisoft/yii2": "~2.0.33",
            "yiisoft/yii2-bootstrap": "~2.0.9",
            "bower-asset/bootstrap": "~3.4.1",
            "npm-asset/jquery": "^2.2",
            "bower-asset/ladda": "0.9.8",
            "yiisoft/yii2-bootstrap4": "~2.0.8",
            "yiisoft/yii2-swiftmailer": "~2.0.0 || ~2.1.0",
            "sjaakp/yii2-pluto": "*",
            "himiklab/yii2-recaptcha-widget" : "*",
            "bizley/cookiemonster": "*",
            "vlucas/phpdotenv": "*",
            "insolita/yii2-migration-generator": "~3.1",        
            "ifsnop/mysqldump-php": "*",
            "supplyhog/yii2-clipboardjs" : "dev-master",
            "devgroup/yii2-tag-dependency-helper": "*",
            "warrence/yii2-kartikgii": "dev-master",
            "kartik-v/yii2-bootstrap4-dropdown": "@dev",
            "kartik-v/yii2-editable": "@dev",
            "kartik-v/yii2-grid":"@dev",
            "kartik-v/yii2-widget-timepicker": "@dev",
            "kartik-v/yii2-date-range": "*",
            "kartik-v/yii2-social": "@dev",
            "kartik-v/yii2-dynagrid": "dev-master",
            "kartik-v/yii2-tree-manager": "@dev",
            "kartik-v/yii2-mpdf":"dev-master",
            "kartik-v/bootstrap-star-rating": "@dev",
            "kartik-v/yii2-slider": "dev-master",
            "kartik-v/yii2-number" : "@dev",
            "mpdf/mpdf":"~8.0.0",
            "2amigos/yii2-google-maps-library" : "*",
            "2amigos/yii2-ckeditor-widget" : "~2.1",
            "seostats/seostats": "dev-master",
            "linslin/yii2-curl":"*",
            "paypal/rest-api-sdk-php": "*",
            "monolog/monolog":"*",
            "ruskid/yii2-csv-importer": "dev-master",
            "phpoffice/phpspreadsheet":"*", 
            "league/omnipay": "*",
            "paragonie/random_compat": "*",
            "symfony/process": "3.0.*@dev",
            "google/cloud-translate": "*"
          },
            "require-dev": {
               "yiisoft/yii2-debug": "~2.1.0",
               "yiisoft/yii2-gii": "~2.1.0",
               "yiisoft/yii2-faker": "~2.0.0",
               "codeception/codeception": "^4.0",
               "codeception/verify": "~0.5.0 || ~1.1.0"
          },
          "config": {
               "process-timeout": 1800
          },
         "fxp-asset": {
               "installer-paths": {
               "npm-asset-library": "vendor/npm-asset",
               "bower-asset-library": "vendor/bower-asset"
              }
          },
         "repositories": [
         {
            "type": "composer",
            "url": "https://asset-packagist.org"
         }
        ], 
       "scripts": {
            "post-install-cmd": "php init --env=Development --overwrite=n"
       }
    }
    

Upvotes: 0

Related Questions