22. Laravel Stub 定制

Stub 定制

Laravel 中可以适用 Artisan的make命令创建各种类, 比如 make:controller 、 make:model 等。 这些类是使用 stub文件生成的。

可以通过 stub:publish 命令将最常见的 Stub 命令发布到你的应用程序中, 方便更改

root@7340a0562010:/var/www/ogenes/Genes-Admin# php81 artisan  stub:publish
root@7340a0562010:/var/www/ogenes/Genes-Admin# ls -l stubs/
total 156
-rw-r--r-- 1 root root  504 Sep  6 10:10 cast.inbound.stub
-rw-r--r-- 1 root root  821 Sep  6 10:10 cast.stub
-rw-r--r-- 1 root root  519 Sep  6 10:10 console.stub
…………

1. 修改Stub命令

以 make:request 命令, 生成的request 经过代码结构改造都需要重构方法 failedValidation(Validator $validator), 这里就可以做以下调整:

基类:

#vim app/Http/Requests/BaseRequest.php
<?php

namespace App\Http\Requests;

use App\Exceptions\CommonException;
use App\Exceptions\ErrorCode;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;

class BaseRequest extends FormRequest
{
 /**
  * Desc:
  * @param Validator $validator
  * @return void
  * @throws CommonException
  * @author: ogenes <ogenes.yi@gmail.com>
  */
 public function failedValidation(Validator $validator): void
 {
     throw new CommonException(
         ErrorCode::INVALID_ARGUMENT,
         $validator->errors()->first()
     );
 }
}

修改:

#vim stubs/request.stub

<?php

namespace {{ namespace }};

use App\Http\Requests\BaseRequest;

class {{ class }} extends BaseRequest
{

 /**
  * Get the validation rules that apply to the request.
  *
  * @return array<string, mixed>
  */
 public function rules()
 {
     return [
     
     ];
 }
}

测试:

root@7340a0562010:/var/www/ogenes/Genes-Admin# php artisan make:request T/TestRequest
<?php

namespace App\Http\Requests\T;

use App\Http\Requests\BaseRequest;

class TestRequest extends BaseRequest
{

 /**
  * Get the validation rules that apply to the request.
  *
  * @return array<string, mixed>
  */
 public function rules()
 {
     return [

     ];
 }
}

2. 自定义Stub命令

考虑到需要经常创建service, 这里创建一个service模板

自定义文件:

#vim stubs/service.stub
<?php

namespace {{ namespace }};

use {{ rootNamespace }}Services\BaseService;

class {{ class }} extends BaseService
{

}

自定义命令行:

#vim app/Console/Tools/ServiceMakeCommand.php
<?php

namespace App\Console\Tools;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'make:service')]
class ServiceMakeCommand extends GeneratorCommand
{
 /**
  * The console command name.
  *
  * @var string
  */
 protected $name = 'make:service';

 /**
  * The console command description.
  *
  * @var string
  */
 protected $description = 'Create a new service class';

 /**
  * The type of class being generated.
  *
  * @var string
  */
 protected $type = 'Service';

 /**
  * Get the stub file for the generator.
  *
  * @return string
  */
 protected function getStub()
 {
     return base_path() . '/stubs/service.stub';
 }

 /**
  * Get the default namespace for the class.
  *
  * @param  string  $rootNamespace
  * @return string
  */
 protected function getDefaultNamespace($rootNamespace)
 {
     return $rootNamespace.'\Services';
 }

 /**
  * Build the class with the given name.
  *
  * Remove the base controller import if we are already in the base namespace.
  *
  * @param  string  $name
  * @return string
  */
 protected function buildClass($name)
 {
     $controllerNamespace = $this->getNamespace($name);

     $replace["use {$controllerNamespace}\BaseService;\n"] = '';

     return str_replace(
         array_keys($replace), array_values($replace), parent::buildClass($name)
     );
 }
}


注册命令

#vim app/Console/Kernel.php
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
        $this->load(__DIR__.'/Tools');

        require base_path('routes/console.php');
    }

测试

root@7340a0562010:/var/www/ogenes/Genes-Admin# php artisan make:service TestService

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值