第二天:新闻文章模块 [云创平台]
准备工作
我想把系统做得简单好用,最好是能节约开发时间。经过考虑,我决定把后端模块化,做成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');
});
配置新闻文章的模块的菜单和权限
导入一个之前使用的整合命令,生成模型和admin控制器
`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 协议》,转载必须注明作者和本文链接
推荐文章: