Laravel如何自定義command命令淺析
前言
用過Laravel的都知道,Laravel通過php artisan make:controller可以生成控制器,同樣的夜可以用命令生成中間介和模型,那怎么自定義生成文件呢?
下面話不多說了,來一起看看詳細的介紹吧
自定義方法如下:
1.創(chuàng)建command類
<?php namespace App\Console\Commands; use Illuminate\Console\GeneratorCommand; 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 = 'Services'; /** * Get the stub file for the generator. * * @return string */ protected function getStub() { return __DIR__.'/stubs/service.stub'; } /** * Get the default namespace for the class. * * @param string $rootNamespace * @return string */ protected function getDefaultNamespace($rootNamespace) { return $rootNamespace."\Services"; } }
2.在Commands/stubs文件下創(chuàng)建自定義模板文件
<?php namespace DummyNamespace; class DummyClass { public function __construct() { } }
創(chuàng)建了一個只有構造函數(shù)的類,具體模板可以自己定義
運行測試
php artisan make:service Web/TestService
這個時候Services文件下的Web目錄下會生成TestService文件,Web目錄不存在時會自動創(chuàng)建
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
PHP中將ip地址轉(zhuǎn)成十進制數(shù)的兩種實用方法
現(xiàn)在PHP中有很多時候都會用到ip地址,但是這個ip地址獲取的時候都不是10進制的。那么PHP中如何將ip地址轉(zhuǎn)成十進制數(shù),下面為大家介紹下兩種方法可以輕松實現(xiàn)2013-08-08CodeIgniter自定義控制器MY_Controller用法分析
這篇文章主要介紹了CodeIgniter自定義控制器MY_Controller用法,結合實例形式分析了CodeIgniter自定義控制器MY_Controller的定義與使用技巧,需要的朋友可以參考下2016-01-01如何通過PHP安裝數(shù)據(jù)庫并使數(shù)據(jù)初始化
這篇文章主要介紹了如何通過PHP安裝數(shù)據(jù)庫并使數(shù)據(jù)初始化,其實原理就是通過構建原生SQL來創(chuàng)建并初始化數(shù)據(jù)庫,連接數(shù)據(jù)庫、創(chuàng)建數(shù)據(jù)庫、使用數(shù)據(jù)庫、創(chuàng)建數(shù)據(jù)表、新增數(shù)據(jù)、關閉連接,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-03-03destoon實現(xiàn)首頁顯示供應、企業(yè)、資訊條數(shù)的方法
這篇文章主要介紹了destoon實現(xiàn)首頁顯示供應、企業(yè)、資訊條數(shù)的方法,在模板開發(fā)中非常實用,需要的朋友可以參考下2014-07-07YII2框架中使用yii.js實現(xiàn)的post請求
本文給大家介紹的是簡單分析下用yii2的yii\helpers\Html類和yii.js實現(xiàn)的post請求的方法,非常的簡單,有需要的小伙伴可以參考下2017-04-04