PHP code example of mhndev / yii2-media

1. Go to this page and download the library: Download mhndev/yii2-media library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mhndev / yii2-media example snippets


namespace app\models;

use mhndev\yii2Media\Interfaces\iEntity;
use mhndev\yii2Media\Traits\EntityTrait;
use yii\db\ActiveRecord;

/**
 * Class Post
 * @package app\models
 */
class Post extends ActiveRecord implements iEntity
{

    use EntityTrait;

    /**
     * @return string
     */
    public static function tableName()
    {
        return 'posts';
    }



}



return [
    'mediaClass' => \mhndev\yii2Media\Models\Media::class,


    'userClass' => \app\modules\user\models\User::class,

    'formats' => [


        'image'=>[
            'avatar'=>[
                'storagePath'=> Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'avatar',
                'uploadSizeLimit' => 2,
            ],

            'post-cover' => [
                'storagePath' => Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'post/cover',
                'uploadSizeLimit' => 3
            ],


            'cover' => [
                'storagePath' => Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'cover',
                'uploadSizeLimit' => 3
            ],

            'post-list-view' => [
                'storagePath' => Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'post/list',
                'uploadSizeLimit' => 4
            ]
        ],

        'audio'=>[
            'music'=>[
                'storagePath'=> Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'music',
                'uploadSizeLimit' => 10

            ],
            'english-learning'=>[
                'storagePath'=> Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'english-learning',
                'uploadSizeLimit' => 4

            ]
        ],

        'video'=>[
            'storagePath'=> Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'video',
            'uploadSizeLimit' => 10
        ],


        'text'=>[

            'document'=>[
                'storagePath'=> Yii::getAlias('@webroot').DIRECTORY_SEPARATOR.'text',
                'uploadSizeLimit' => 1
            ]
        ],

    ],


    'min_storage' => 100


];



    /**
     * @param string $media_id
     * @return mixed
     */
    public static function markMediaAsDefaultByMediaId($media_id)
    {
        $mediaClass = self::mediaClass();

        /** @var iMedia $media */
        $media = $mediaClass::findOne($media_id);

        return $media->markAsDefault();
    }

    /**
     * @return array|string
     */
    public function actionUploadAndAttachMedia()
    {
        $data = Yii::$app->request->post();
        $model = $data['entity']::findOne($data['entity_id']);

        return ['records'=>Media::storeAndAttach('media', $data['type'], $model)];
    }