How to create a custom artisan make command with Laravel new class file

Source: Internet
Author: User
The following article mainly introduces you to the Laravel how to create a custom artisan make command to new class files related materials, the need for friends can refer to, let's take a look at it.

Laravel provides powerful console commands to handle non-browser business logic through artisan.

Objective

This article is mainly about Laravel by creating a custom artisan make command to create a new class file related content, share it for everyone to reference the study, the following words do not say, come together to see the detailed introduction.

In Laravel development, we often use artisan make:controller such commands to create new controllers, Model, Job, event and other class files. In Laravel5.2, artisan make command support to create the following file:

Make:auth   Scaffold Basic Login and registration views and routes Make:console  Create a new Artisan command Make:co Ntroller  Create a new controller class Make:event   Create a new event class Make:job   Create a new job class Mak E:listener  Create a new event listener class Make:middleware  Create a new middleware class Make:migration  Cre Ate a new migration file Make:model   Create a new eloquent model class Make:policy   Create a new policy class Make:p  Rovider  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

However, sometimes the default does not meet our needs, for example, we use the Respository model in the project to further encapsulate the model file, it is necessary to create repository class files Often, long time will be able to pass artisan make:repository The command automatically creates the class file instead of each time it is created manually.

The system comes with the artisan make command corresponding PHP program placed in the Illuminate\foundation\console directory, we refer to illuminate\foundation\console\ The Providermakecommand class to define its own artisan make:repository commands.

First, create the command class

Under the App\console\commands folder, create the repositorymakecommand.php file with the following procedure:

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 Gen erated.  *  * @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 '; }}

Second, create a template file corresponding to the command class

Create a template file under App\console\commands\stubs. The stub file is the template for the class file generated by the make command, which defines the generic part of the class file to be generated to create the repository.stub template file:

namespace Dummynamespace;  Use app\repositories\baserepository;  Class Dummyclass extends Baserepository {    /**   * Specify Model class name   *    * @return string  */ Public function model ()  {   //set model name ' Here ', this is necessary!  }}

Third, the registration command class

Add Repositorymakecommand to app\console\kernel.php

protected $commands = [  commands\repositorymakecommand::class];

Test command

OK, now you can make:repository create the Repository class file by command.

PHP artisan make:repository testrepositoryphp Artisan make:repository subdirectory/testrepository

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.