alexs/yii2-alexgallery

Gallery for Yii2 Framework.

Installs: 149

Dependents: 0

Suggesters: 0

Security: 0

Type:yii2-extension

1.4.2 2023-01-14 12:02 UTC

This package is auto-updated.

Last update: 2024-11-14 16:12:47 UTC


README

image title

Create a table

Run the migration

yii migrate --migrationPath=@vendor/alexs/yii2-alexgallery/src/migrations

or execute SQL-code

CREATE TABLE `gallery` (
  `id` int(11) NOT NULL,
  `image` varchar(255) DEFAULT NULL,
  `pos` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT;

ALTER TABLE `gallery`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `gallery`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

Create a model

<?php
namespace app\models;

class Gallery extends \alexs\alexgallery\models\Gallery
{

}

Create a controller

<?php
namespace app\modules\admin\controllers;

class GalleryController extends \alexs\alexgallery\controllers\GalleryController
{
    public $enableCsrfValidation = false; // remove if a widget used inside active form

    public function actionIndex() {
        return $this->render('gallery', [

        ]);
    }
}

Init the widget in a template of your gallery

AlexGallery::widget([
    'Model'=>new Gallery,
    'upload_url'=>'/admin/gallery/upload',
    'delete_url'=>'/admin/gallery/delete',
    'sort_url'=>'/admin/gallery/sort',
])

If you need a relation

Add a foreign key article_id to gallery table in the database

<?php
namespace app\models;

class ArticleGallery extends \alexs\alexgallery\models\Gallery
{
    /**
     * @inheritDoc
     */
    public static function getGalleryRelationAttribute() {
        return 'article_id';
    }
}
<?php
namespace app\modules\admin\controllers;

class GalleryController extends \alexs\alexgallery\controllers\GalleryController
{
    public function actionIndex() {
        return $this->render('gallery', [

        ]);
    }
}
<?php
AlexGallery::widget([
    'Model'=>new ArticleGallery,
    'RelationModel'=>$ArticleModel,
    'upload_url'=>'/admin/article-gallery/upload',
    'delete_url'=>'/admin/article-gallery/delete',
    'sort_url'=>'/admin/article-gallery/sort'
])