亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

laravel通過創(chuàng)建自定義artisan make命令來新建類文件詳解

 更新時間:2017年08月17日 10:57:56   作者:GreenLightt  
Laravel通過Artisan提供了強大的控制臺命令來處理非瀏覽器業(yè)務(wù)邏輯。下面這篇文章主要給大家介紹了關(guān)于laravel如何通過創(chuàng)建自定義artisan make命令來新建類文件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。

前言

本文主要跟大家介紹的是關(guān)于laravel通過創(chuàng)建自定義artisan make命令來新建類文件的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

我們在laravel開發(fā)時經(jīng)常用到artisan make:controller等命令來新建Controller、Model、Job、Event等類文件。 在Laravel5.2中artisan make命令支持創(chuàng)建如下文件:

 make:auth   Scaffold basic login and registration views and routes
 make:console  Create a new Artisan command
 make:controller  Create a new controller class
 make:event   Create a new event class
 make:job   Create a new job class
 make:listener  Create a new event listener class
 make:middleware  Create a new middleware class
 make:migration  Create a new migration file
 make:model   Create a new Eloquent model class
 make:policy   Create a new policy class
 make:provider  Create a new service provider class
 make:request  Create a new form request class
 make:seeder   Create a new seeder class
 make:test   Create a new test class

不過,有時候默認(rèn)的并不能夠滿足我們的需求, 比方我們在項目中使用的Respository模式來進一步封裝了Model文件,就需要經(jīng)常創(chuàng)建Repository類文件了,時間長了就會想能不能通過artisan make:repository命令自動創(chuàng)建類文件而不是都每次手動創(chuàng)建。

系統(tǒng)自帶的artisan make命令對應(yīng)的PHP程序放在Illuminate\Foundation\Console目錄下,我們參照Illuminate\Foundation\Console\ProviderMakeCommand類來定義自己的artisan make:repository命令。

一、創(chuàng)建命令類

在app\Console\Commands文件夾下創(chuàng)建RepositoryMakeCommand.php文件,具體程序如下:

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class RepositoryMakeCommand extends GeneratorCommand
{
 /**
  * The console command name.
  *
  * @var string
  */
 protected $name = 'make:repository';

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

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

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

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

二、創(chuàng)建命令類對應(yīng)的模版文件

在app\Console\Commands\stubs下創(chuàng)建模版文件 .stub文件是make命令生成的類文件的模版,用來定義要生成的類文件的通用部分創(chuàng)建repository.stub模版文件:

 namespace DummyNamespace;
 
 use App\Repositories\BaseRepository;
 
 class DummyClass extends BaseRepository
 {
  
  /**
   * Specify Model class name
   * 
   * @return string
   */
  public function model()
  {
   //set model name in here, this is necessary!
  }
 }

三、注冊命令類

將RepositoryMakeCommand添加到App\Console\Kernel.php中

 protected $commands = [
  Commands\RepositoryMakeCommand::class
 ];

測試命令

好了, 現(xiàn)在就可以通過make:repository命令來創(chuàng)建repository類文件了

php artisan make:repository TestRepository

php artisan make:repository SubDirectory/TestRepository 

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論