alexs / yii2-alexgallery

为 Yii2 框架的图库。

安装: 148

依赖者: 0

建议者: 0

安全: 0

类型:yii2-extension

1.4.2 2023-01-14 12:02 UTC

This package is auto-updated.

Last update: 2024-09-14 15:46:59 UTC


README

image title

创建一个表

运行迁移

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

或执行 SQL 代码

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;

创建一个模型

<?php
namespace app\models;

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

}

创建一个控制器

<?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', [

        ]);
    }
}

在你的图库模板中初始化小部件

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

如果你需要一个关系

在数据库中图库表中添加外键 article_id

<?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'
])