Laravel 半自动代码生成器

1、创建 command 命令

创建 Controller模板、Service 模板、Model 模板、Api Resource模板

1. php artisan make:command MakeController  # Controller模板
2. php artisan make:command MakeService #Service 模板
3. php artisan make:command MakeModel #Model 模板
4. php artisan make:command MakeResource #Api Resource模板
5. php artisan make:command MakeCollection #Api Resource模板
6. php artisan make:command MakeModule #其他模板集合

生成效果如下:
在这里插入图片描述

2、各个命令文件详情

1、MakeController

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeController extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:controller {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成controller对象类';

    protected $type = "Controller";


    protected function getStub()
    {
        return base_path("stubs\controller.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Controllers\V1";
    }
}

2、MakeService

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeService extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:service {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成service对象类';

    protected $type = "Service";


    protected function getStub()
    {
        return base_path("stubs\\service.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Repositories\Services";
    }
}

3、MakeModel

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeModel extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:model {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成model对象类';

    protected $type = "Model";


    protected function getStub()
    {
        return base_path("stubs\\model.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Repositories\Models";
    }
}

4、MakeResource

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeResource extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:resource {name} {raw-name?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成resource-collection对象类';

    protected $type = "Resource";


    protected function getStub()
    {
        return base_path("stubs\\resource\\resource.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
    }
}

5、MakeCollection

<?php

namespace App\Console\Commands\Generator;


use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;

class MakeCollection extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generator:collection {name} {raw-name?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成resource-collection对象类';

    protected $type = "Collection";


    protected function getStub()
    {
        return base_path("stubs\\resource\\collection.stub");
    }

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace."\Http\Resources\\".$this->argument("raw-name")?:$this->argument("name");
    }
}

6、MakeModule

<?php

namespace App\Console\Commands;


use App\Console\Commands\Generator\MakeCollection;
use App\Console\Commands\Generator\MakeController;
use App\Console\Commands\Generator\MakeModel;
use App\Console\Commands\Generator\MakeResource;
use App\Console\Commands\Generator\MakeService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class MakeModule extends Command
{
    /**
     * 控制台命令的名称和签名
     *
     * @var string
     */
    protected $signature = 'generator:module {name}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = '生成模板文件';


    public function handle()
    {
        $this->generatorController();
        $this->generatorModel();
        $this->generatorService();
        $this->generatorResource();
    }


    /**
     * 生成 Controller 类
     */
    private function generatorController()
    {
        $name = $this->argument("name");
        Artisan::call(MakeController::class,['name'=>$name.'Controller']);
        $this->line("Generator Controller Success");
    }

    /**
     * 生成 Service 类
     */
    private function generatorService()
    {
        $name = $this->argument("name");
        Artisan::call(MakeService::class,['name'=>$name.'Service']);
        $this->line("Generator Service Success");
    }

    /**
     * 生成 Model 类
     */
    private function generatorModel()
    {
        $name = $this->argument("name");
        Artisan::call(MakeModel::class,['name'=>$name]);
        $this->line("Generator Model Success");
    }

    /**
     * 生成资源类
     */
    private function generatorResource()
    {
        $name = $this->argument("name");
        Artisan::call(MakeResource::class,['name'=>$name.'Resource','raw-name'=>$name]);
        Artisan::call(MakeCollection::class,['name'=>$name.'Collection','raw-name'=>$name]);
        $this->line("Generator Resource Success");
    }


}

3、创建 stub 模板文件

模板目录文件结构:
在这里插入图片描述
1、controller.stub

<?php

namespace DummyNamespace;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class DummyClass extends Controller
{

    public function index()
    {
        //
    }


    public function store(Request $request)
    {
        //
    }


    public function show($id)
    {
        //
    }


    public function update(Request $request, $id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }
}

2、service.stub

<?php

namespace DummyNamespace;

class DummyClass
{
    //
}

3、model.stub

<?php

namespace DummyNamespace;

use Illuminate\Database\Eloquent\Factories\HasFactory;


class DummyClass extends Model
{
    use HasFactory;

    public $guarded=["*"];

}

4、resource.stub

<?php

namespace DummyNamespace;

use Illuminate\Http\Resources\Json\JsonResource;

class DummyClass extends JsonResource
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }

}

5、collection.stub

<?php

namespace DummyNamespace;

use Illuminate\Http\Resources\Json\ResourceCollection;

class DummyClass extends ResourceCollection
{
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}

4、查看 Artisan 命令

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Laravel是一种流行的PHP框架,用于构建Web应用程序。RBAC(Role-Based Access Control)是一种在应用程序中管理用户权限的方法。在Laravel中,可以使用API代码来实现RBAC。 实现RBAC的第一步是定义角色和权限。在Laravel中,可以使用数据库表来存储角色和权限的信息。可以创建一个名为roles的表来存储角色信息,包括角色名称和可访问的路由。还可以创建一个名为permissions的表来存储权限信息,包括权限名称和对应的路由。 接下来,需要在Laravel中定义路由,用来处理权限的验证和角色的分配。可以创建一个名为'check.permission'的中间件,用于验证用户的权限。在这个中间件中,可以查询数据库,获取当前用户的角色和可访问的路由,然后进行权限验证。如果用户没有权限访问该路由,则返回相应的错误信息。 当用户登录后,可以为用户分配一个或多个角色。在Laravel中,可以使用Session来存储用户信息。可以创建一个名为'assign.role'的路由,用于接收用户角色的分配请求。在这个路由中,可以将用户ID和角色ID存储到数据库中,并将用户的角色信息存储到Session中。 最后,可以创建一个名为'check.role'的中间件,用于验证用户的角色。在这个中间件中,可以查询数据库,获取当前用户的角色信息,并与当前访问的路由所需的角色进行比较。如果用户不具备所需的角色,则返回相应的错误信息。 通过以上步骤,可以实现基于角色和权限的访问控制。在Laravel中使用RBAC API代码,可以有效地管理和控制用户的权限,确保应用程序的安全性和完整性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲁元

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值