PHP code example of fonqing / aoma

1. Go to this page and download the library: Download fonqing/aoma 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/ */

    

fonqing / aoma example snippets



namespace app\controller;

use aoma\fast\BaseController;
use aoma\fast\traits\AutoCrud;
use aoma\fast\traits\Authorize;
use app\model\News;
use app\model\User;


class NewsController extends BaseController
{
    use AutoCrud, Authorize;

    public function setup()
    {
        // 开启自动CRUD操作时,需要设置当前控制器操作的模型类
        $this->setModel(News::class);
        // 设置当前session的用户模型实例
        // 必须继承\aoma\fast\BaseModel类,
        // 且实现\aoma\fast\UserInterface接口
        $this->session->set(User::find(1));
    }

    // 启用自动CRUD后, 以下 action 即可自动工作
    /*
     news/create 
     news/update 
     news/index 
     news/delete 
     news/detail 
     news/export (需要在模型配置导出字段)
     */ 
     
     /**
      * 自定义创建数据前的操作
      * @param array $data 数据数组
      */
     public function beforeCreate($data) {
        $data['user_id'] = $this->session->getUserId();
        return $data;
     }
}


namespace app\model;

use aoma\fast\BaseModel;

class News extends BaseModel {
    protected $table = 'news';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $dateFormat = 'Y-m-d H:i:s';
   /**
     * Model fields definition, used for search, create, update, index.
     * 
     * @var array
     */
    public static array $fields = [
        'create' => ['cate_id', 'title', 'cover_image', 'content'],// allow to create fields
        'update' => ['cate_id', 'title', 'cover_image', 'content'], // allow update fields
        'index'  => ['id','cate_id', 'title', 'cover_image', 'content', 'create_time'], // allow index fields
    ];

    /**
     * Model data validation rules, used for create and update.
     *
     * @var array
     */
    public static array $rules = [
        'create' => [
            'cate_id' => '否开启缓存
}


namespace app\model\export;

trait NewsExport {
    public function getExportConfig(): array
    {
       return [
           'title'   => '文章列表',
           'name'    => '文章列表_' . date('YmdHis'),
           'columns' => [
               '_index'   => ['width' => 5, 'title' => '序号'], //
                'cate_id' => [
                    'width' => 10,
                    'title' => '分类',
                    'callback' => function ($row) {
                        $cates = [4 => '新闻', 2 => '通知', 3 => '公告'];
                        return $cates[$row['cate_id']] ?? '未知';
                    }
                ],
                'title' => ['width' => 30, 'title' => '标题'],
                'create_time' => ['width' => 20, 'title' => '发布时间'],
           ]
       ];
    }
}


namespace app\model;

use aoma\fast\BaseModel;
use app\model\export\NewsExport;

class News extends BaseModel {
    use NewsExport;
}