yii2php-7.3

yii\base\ErrorException Use of undefined constant logo - assumed 'logo' (this will throw an Error in a future version of PHP)


I installed web apps for Library Management, when I try to upload logo for the library, it give me error like this

Unknown Property – yii\base\UnknownPropertyException Use of undefined constant logo - assumed 'logo' (this will throw an Error in a future version of PHP)

This is my stack trace:

1. in /otka/app/src/web/backend/modules/setting/umum/controllers/DataPerpustakaanController.php at line 44
// $model->NamaLokasiPerpustakaan = Yii::$app->config->get('NamaLokasiPerpustakaan');

$model->JenisPerpustakaan = Yii::$app->config->get('JenisPerpustakaan');
$model->IsUseKop = Yii::$app->config->get('IsUseKop');


if ($model->load(Yii::$app->request->post())) {
    $model->logo = UploadedFile::getInstance($model, 'logo');

    $temp_logo = ($model->logo != "" ? logo : kop);

    $model->image = UploadedFile::getInstance($model, $temp_logo);
    if($model->image!='')
        {
            $files_uploaded = '../uploaded_files/aplikasi/' . "temp_image" . '.' . "png";
            $model->image->saveAs($files_uploaded,false);
            $mimetype=DirectoryHelpers::mimeType($files_uploaded);
            if ($mimetype){
                unlink($files_uploaded);
2.  in /otka/app/src/web/backend/modules/setting/umum/controllers/DataPerpustakaanController.php at line 44– yii\base\ErrorHandler::handleError(2, 'Use of undefined constant logo -...', '/otka/app/src/web/backend/module...', 44, ...)
3. backend\modules\setting\umum\controllers\DataPerpustakaanController::actionIndex()
4. in /otka/app/src/web/vendor/yiisoft/yii2/base/InlineAction.php at line 55- call_user_func_array([backend\modules\setting\umum\controllers\DataPerpustakaanController, 'actionIndex'], [])
5. in /otka/app/src/web/vendor/yiisoft/yii2/base/Controller.php at line 154– yii\base\InlineAction::runWithParams([])
6. in /otka/app/src/web/vendor/yiisoft/yii2/base/Module.php at line 454– yii\base\Controller::runAction('index', [])
7. in /otka/app/src/web/vendor/yiisoft/yii2/web/Application.php at line 84– yii\base\Module::runAction('setting/umum/data-perpustakaan/i...', [])
8. in /otka/app/src/web/vendor/yiisoft/yii2/base/Application.php at line 375– yii\web\Application::handleRequest(yii\web\Request)

This is the function line in DataPerpustakaanController.php

<?php
namespace backend\modules\setting\umum\controllers;

use Yii;
use yii\base\DynamicModel;
use yii\web\UploadedFile;
use common\components\DirectoryHelpers;

class DataPerpustakaanController extends \yii\web\Controller
{
    public function actionIndex()
    {
        $model = new DynamicModel([
            'NamaPerpustakaan',
            //'NamaLokasiPerpustakaan',
            'JenisPerpustakaan',
            'IsUseKop',
            'logo',
            'kop',
            'image',
        ]);
        $model->addRule([
            'NamaPerpustakaan',
            //'NamaLokasiPerpustakaan',
            'JenisPerpustakaan', 'IsUseKop',], 'required'
        );

        $model->NamaPerpustakaan = Yii::$app->config->get('NamaPerpustakaan');
        // $model->NamaLokasiPerpustakaan = Yii::$app->config->get('NamaLokasiPerpustakaan');

        $model->JenisPerpustakaan = Yii::$app->config->get('JenisPerpustakaan');
        $model->IsUseKop = Yii::$app->config->get('IsUseKop');
        if ($model->load(Yii::$app->request->post())) {
            $model->logo = UploadedFile::getInstance($model, 'logo');
         
            $temp_logo = ($model->logo != "" ? logo : kop); //**This is line 44**

            $model->image = UploadedFile::getInstance($model, $temp_logo);
            if($model->image!='')
            {
                $files_uploaded = '../uploaded_files/aplikasi/' . "temp_image" . '.' . "png";
                $model->image->saveAs($files_uploaded,false);
                $mimetype=DirectoryHelpers::mimeType($files_uploaded);
                if ($mimetype){
                    unlink($files_uploaded);
                        if ($model->logo != "") {
                            $model->image->saveAs('../uploaded_files/aplikasi/' . "logo_perpusnas_2015" . '.' . "png");                        
                        } else{
                            $model->image->saveAs('../uploaded_files/aplikasi/' . "kop" . '.' . "png");
                        }
                }
                else {
                Yii::$app->getSession()->setFlash('error', [
                        'type' => 'error',
                        'duration' => 500,
                        'icon' => 'fa fa-info-circle',
                        'message' => Yii::t('app', 'Failed Save'),
                        'title' => 'Info',
                        'positonY' => Yii::$app->params['flashMessagePositionY'],
                        'positonX' => Yii::$app->params['flashMessagePositionX']
                    ]);

                }
            }
            
                if ($model->validate()) {
                    Yii::$app->config->set('NamaPerpustakaan', Yii::$app->request->post('DynamicModel')['NamaPerpustakaan']);
                    //Yii::$app->config->set('NamaLokasiPerpustakaan', Yii::$app->request->post('DynamicModel')['NamaLokasiPerpustakaan']);
                    Yii::$app->config->set('JenisPerpustakaan', Yii::$app->request->post('DynamicModel')['JenisPerpustakaan']);
                    Yii::$app->config->set('IsUseKop', Yii::$app->request->post('DynamicModel')['IsUseKop']);


                    Yii::$app->getSession()->setFlash('success', [
                        'type' => 'info',
                        'duration' => 500,
                        'icon' => 'fa fa-info-circle',
                        'message' => Yii::t('app', 'Success Save'),
                        'title' => 'Info',
                        'positonY' => Yii::$app->params['flashMessagePositionY'],
                        'positonX' => Yii::$app->params['flashMessagePositionX']
                    ]);
                } 
                else {
                Yii::$app->getSession()->setFlash('error', [
                        'type' => 'error',
                        'duration' => 500,
                        'icon' => 'fa fa-info-circle',
                        'message' => Yii::t('app', 'Failed Save'),
                        'title' => 'Info',
                        'positonY' => Yii::$app->params['flashMessagePositionY'],
                        'positonX' => Yii::$app->params['flashMessagePositionX']
                    ]);
                }
            return $this->redirect(['index']);
        } else {
            return $this->render('index', [
                'model' => $model,]);
        }
    }

}

How to fix this problem?


Solution

  • I solve this problem, so in line 44 there is code

    $temp_logo = ($model->logo != "" ? logo : kop);
    

    change it like this

    $temp_logo = ($model->logo != "" ? 'logo' : 'kop');