Mostafa Talebi
Mostafa Talebi

Reputation: 9183

PHP Artisan Custom Command

I need a custom command in my php artisan.

I have created the command file via artisan shell.

But when I execute it via php artisan it throws it as undefined.

It is also confusing for me how to add arguments to my command.

Here is my whole command file:

<?php

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class createLibrarySet extends Command
{

/**
 * The console command name.
 *
 * @var string
 */
protected $name = 'createLibrarySet';

/**
 * The console command description.
 *
 * @var string
 */
protected $description = 'Creates a Library Set.';

/**
 * Create a new command instance.
 *
 * @return void
 */
public function __construct()
{
    parent::__construct();
}

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function fire()
{
    $dir = mkdir(app_path()."/".library."/".$this->library_name);
    $coreName = $this->library_name;
    $serivceProvider = $coreName."."."ServiceProvider.php";
    $library = $coreName."."."Library.php";
    $facade = $coreName."."."Facade.php";
    $interface = $coreName."."."Interface.php";
    fopen($dir."/".$library, "w");
    fopen($dir."/".$facade, "w");
    fopen($dir."/".$serivceProvider, "w");
    fopen($dir."/".$interface, "w");
}

/**
 * Get the console command arguments.
 *
 * @return array
 */
protected function getArguments()
{
    return array(
        array('example', InputArgument::REQUIRED, 'An example argument.'),
    );
}

/**
 * Get the console command options.
 *
 * @return array
 */
protected function getOptions()
{
    return array(
        array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
    );
}

}

Upvotes: 1

Views: 2492

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You have to add the command to your app/start/artisan.php:

Artisan::resolve('createLibrarySet');

Now you should be able to see in the

php artisan

listing.

To get your arguments, you just have to:

$email = $this->argument('email');

And options

$force = $this->option('force');

Upvotes: 3

Related Questions