D
D
Daniil Sidorov2016-10-03 20:20:51
Yii
Daniil Sidorov, 2016-10-03 20:20:51

How to properly connect external modules in Yii2?

I'm trying to include Yii2-images. I did everything as described in the instructions :

  1. Installed via composer
  2. Performed migrations
  3. Registered the module in web.php
  4. Added to the behaviors model

In the controller ProductController I try to use the attachImage extension method , but it is highlighted, saying that it does not know this method.
Here is the controller code (actionCreate):
public function actionCreate()
    {
        $model = new Product();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            $model->image = UploadedFile::getInstance($model, 'image');
            if($model->image)
            {
                $path = Yii::getAlias('@webroot/upload/files').$model->image->baseName.'.'.$model->image->extension;
                $model->image->saveAs($path);
                $model->attachImage($path);
            }
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

Here's what I'm adding to the controller:
namespace app\modules\admin\controllers;

use Yii;
use app\models\Product;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

What could be wrong? Maybe the autoloader is not working or I messed up with the namespaces?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
melnikov_m, 2016-10-06
@melnikov_m

is highlighted because it is a behavior method, not a class method. You need to write mixin
/**
* @mixin rico\yii2images\behaviors\ImageBehave
*/
then the methods of this behavior will be highlighted as class methods

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question