Where To Save Own Actions And Select Right Namespace?

Where to save self created actions and how to name its namespace?

I am trying to use imperative redactor in yii2, and want to reimplement ImageFileUpload action taken from ‘redactor’ extension made for yii.

I got this error message


ReflectionException


Class yii\imperative\ImageUploadAction does not exist

This is my code where I included this action to controller


public function actions()

    {

        return [

            'imageupload' => [

                'class' => 'yii\imperative\ImageUploadAction',

                'uploadPath' => '/uploads',

//                'uploadUrl' => '/url/to/uploads/folder',

                'uploadCreate' => true,

                'permissions' => 0755,

            ],

        ];

    }

The action class is placed in


/frontend/actions/ImageUploadAction.php

This is a code




<?php


namespace yii\imperative;


use Yii;

use yii\base\Action;

use yii\web\HttpException;

use yii\web\UploadedFile;

use yii\helpers\Json;


/**

 * Redactor widget image upload action.

 *

 * @param string $attr Model attribute

 * @throws CHttpException

 */

class ImageUploadAction extends Action

{


    public $uploadPath;

    public $uploadUrl;

    public $uploadCreate = false;

    public $permissions = 0774;


    public function run($attr)

    {

        $name = strtolower($this->getController()->getId());

        $attribute = strtolower((string) $attr);


        if ($this->uploadPath === null)

        {

            $path = \yii::$app->basePath . DIRECTORY_SEPARATOR . 'uploads';

            $this->uploadPath = realpath($path);

            if ($this->uploadPath === false && $this->uploadCreate === true)

            {

                if (!mkdir($path, $this->permissions, true))

                {

                    throw new HttpException(500, Json::encode(

                        ['error' => 'Could not create upload folder "' . $path . '".']

                    ));

                }

            }

        }

        if ($this->uploadUrl === null)

        {

            $this->uploadUrl = \yii::$app->request->baseUrl . '/uploads';

        }


        // Make Yii think this is a AJAX request.

        $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';


        $file = UploadedFile::getInstanceByName('file');

        if ($file instanceof UploadedFile)

        {

            $attributePath = $this->uploadPath . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . $attribute;

            if (!in_array(strtolower($file->getExtensionName()), array('gif', 'png', 'jpg', 'jpeg')))

            {

                throw new HttpException(500, Json::encode(

                    ['error' => 'Invalid file extension ' . $file->getExtension() . '.']

                ));

            }

            $fileName = trim(md5($attribute . time() . uniqid(rand(), true))) . '.' . $file->getExtension();

            if (!is_dir($attributePath))

            {

                if (!mkdir($attributePath, $this->permissions, true))

                {

                    throw new HttpException(500, Json::encode(

                        ['error' => 'Could not create folder "' . $attributePath . '". Make sure "uploads" folder is writable.']

                    ));

                }

            }

            $path = $attributePath . DIRECTORY_SEPARATOR . $fileName;

            if (file_exists($path) || !$file->saveAs($path))

            {

                throw new HttpException(500, Json::encode(

                    ['error' => 'Could not save file or file exists: "' . $path . '".']

                ));

            }

            $attributeUrl = $this->uploadUrl . '/' . $name . '/' . $attribute . '/' . $fileName;

            $data = array(

                'filelink' => $attributeUrl,

            );

            echo Json::encode($data);

            exit;

        } else

        {

            throw new HttpException(500, Json::encode(

                ['error' => 'Could not upload file.']

            ));

        }

    }


}



The path to the class looks like being wrong. Try to use this: /frontend/actions/ImageUploadAction (or similar, I don’t know your file structure).

Class path should match namespace i.e. in your case it should be frontend/actions.

Thanks to all of you. Finally I got it.