1

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),
    );
}

}
2
  • Why are you doing all those fopen() calls and then failing to capture the file handle? If you intend to actually write to those files, you'll need the filehandle to do so, and you're losing all of them. Commented Apr 4, 2014 at 14:36
  • no that section is incomplete and is not my concern, I'm concerned with artisan interface now Commented Apr 4, 2014 at 14:36

1 Answer 1

3

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');
Sign up to request clarification or add additional context in comments.

1 Comment

may I ask how then I can get the arguments passed in the shell? currently I have only one argument which is "example" but I don't really know how to get its value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.