第二天:新聞文章模組 [雲創平臺]

WangWang發表於2019-05-20

我想把系統做得簡單好用,最好是能節約開發時間。經過考慮,我決定把後端模組化,做成laravel-admin的擴充套件,前端整體做成一個模組。先把核心模組歸納下,後期可能還要加更多模組。

第二天:新聞文章模組【雲創平臺】(學習怎麼寫文章,以前沒寫過)

現在通過命令生成所有模組

//修改config/admin.php
'extension_dir' => base_path('modules'),
//執行命令

#基礎和核心
php artisan admin:extend totoro/core --namespace Totoro\Core
#新聞文章
php artisan admin:extend totoro/news --namespace Totoro\News
#企業管理
php artisan admin:extend totoro/enterprise --namespace Totoro\Enterprise
#資訊管理
php artisan admin:extend totoro/community --namespace Totoro\Community
#人才評測
php artisan admin:extend totoro/evaluation --namespace Totoro\Evaluation
//企業服務
php artisan admin:extend totoro/service --namespace Totoro\Service
//會員管理
php artisan admin:extend totoro/member --namespace Totoro\Member
//使用app做為前端模組。就不執行命令了。

執行完成之後,根目錄下多了這些資料夾
第二天:新聞文章模組【雲創平臺】(正在編輯)
把模組新增到專案composer.json

    "require": {
    //***
  "totoro/core": "dev-master",
  "totoro/news": "dev-master",
  "totoro/community": "dev-master",
  "totoro/enterprise": "dev-master",
  "totoro/evaluation": "dev-master",
  "totoro/service": "dev-master",
  "totoro/member": "dev-master"
},
//***
"repositories": [
  {
    "type": "path",
    "url": "modules/totoro/core"
  },
  {
    "type": "path",
    "url": "modules/totoro/news"
  },
  {
    "type": "path",
    "url": "modules/totoro/community"
  },
  {
    "type": "path",
    "url": "modules/totoro/enterprise"
  },
  {
    "type": "path",
    "url": "modules/totoro/evaluation"
  },
  {
    "type": "path",
    "url": "modules/totoro/service"
  },
  {
    "type": "path",
    "url": "modules/totoro/member"
  }
]

執行composer update

第二天:新聞文章模組【雲創平臺】(正在編輯)

可以開始做新聞模組了

/**
 * 新聞資料表
  */
Schema::create('news', function (Blueprint $table) {
  $table->increments('id');
  $table->unsignedInteger('user_id');
  $table->unsignedInteger('cate_id')->comment('分類ID');
  $table->string('title', 255)->comment('標題');
  $table->string('tags')->nullable()->comment('標籤');
  $table->string('thumbnail', 255)->nullable()->comment('縮圖');
  $table->string('summary', 500)->nullable()->comment('摘要');
  $table->text('content')->comment('內容');
  $table->text('images')->comment('主圖');
  $table->unsignedTinyInteger('status')->comment('狀態:0草稿,1稽核中,2已釋出');
  $table->timestamps();
});

/**
 * 新聞評論表
  */
Schema::create('news_comments', function (Blueprint $table) {
  $table->increments('id');
  $table->unsignedInteger('member_id')->comment('會員ID');
  $table->unsignedInteger('reply_id')->nullable()->comment('回覆ID');
  $table->text('content')->comment('評論內容');
  $table->timestamps();
});

/**
 * 新聞分類表
  */
Schema::create('news_categories', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name')->comment('分類');
  $table->string('slug');
  $table->unsignedInteger('parent_id')->default(0)->comment('上級ID');
  $table->unsignedSmallInteger('sort')->default(0)->comment('排序');
  $table->string('content', 1000);
});

/**
 * 標籤表
  */
Schema::create('news_tags', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name')->comment('標籤名稱');
  $table->string('slug');
});

/**
 * 關聯表 關聯新聞
  */
Schema::create('news_relations', function (Blueprint $table) {
  $table->unsignedInteger('self_id');
  $table->unsignedInteger('target_id');
});

/**
 * 關聯表 分類新聞
  */
Schema::create('news_categories_news', function (Blueprint $table) {
  $table->unsignedInteger('news_id');
  $table->unsignedInteger('cate_id');
});
`namespace App\Console\Commands;

use Illuminate\Console\Command;

class MyModel extends Command
{
/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'model
                        {name : model name}
                        {--skipModel : skip model}
                        {--skipAdmin : skip admin}
                        {--T|table= : database table name}';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = '生成模型及控制器';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{

    $table = $this->option('table');
    $name = $this->argument('name');
    //
    echo "建立模型基類\n";
    $this->call("infyom:model", ["--fromTable" => true, "--tableName" => $table, "model" => $name . "Table"]);

    if (!$this->option('skipModel')) {
        echo "建立資料模型\n";
        $this->call("make:model", ["name" => "App\Models\\$name"]);
        $path = app_path("Model\\$name.php");
        if (realpath($path)) {
            $file = file_get_contents($path);
            $file = str_replace("use Illuminate\Database\Eloquent\Model;", '', $file);
            $file = str_replace("extends Model", 'extends \\App\\Tables\\' . $name . 'Table', $file);
            file_put_contents($path, $file);
        }
    } else {
        echo "跳過建立模型\n";
    }
    echo "建立Admin控制器\n";
    $this->call("admin:make", ['name' => $name . "Controller", '--model' => "App\Models\\$name"]);
    return;
}
}

`

還有其他重要的工作,明天再繼續

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章