Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and using Laravel 4 commands

EDIT: Figured out where I was going wrong and placed an answer at the end

I'm trying to create a Laravel Command, I can see it's changed considerably from "tasks" in Laravel 3. However I can't seem to get it to run. These are the steps I have taken:

php artisan command:make Import

Returns

Command created successfully

The file in the commands directory is then created and I have slightly modified to return "Hello World" like so:

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

class Import extends Command {

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description.';

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

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        return 'Hello World';
    }

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

}

However when I try and run the command like so:

php artisan Import

I get the following error:

[InvalidArgumentException] Command "Import" is not defined.

I have tried it with and without capitals as well as naming it "ImportCommand" since the documentation names its command "FooCommand" but with no luck.

Any help would be most appreciated.

like image 986
John Mellor Avatar asked Jun 18 '13 14:06

John Mellor


People also ask

What are the advanced Laravel commands?

Below are Some of the advanced laravel commands which are as follows: php artisan make: model Project --migration --controller --resource This command is used to create a new migration file for the model(migration), create a new controller for the model(controller) and to have a resource controller for the generated controller.

How to create an API using Laravel?

How to Create an API using Laravel 1. Creating the project. A new Laravel project is created by the command below. You can substitute taskmanager with any... 2. Creating the database. Open Xampp and launch phpMyAdmin. Use it to create a database called tasks. We will create... 3. Migration. We use ...

What are the intermediate Laravel commands?

Some of those kinds of requiring intermediate laravel commands are mentioned below: This command is used to start a laravel project, and by default, the application will be hosted at localhost with port number 8000 This command is used to create a new model class. If we execute the command, php artisan list, we will find a couple of makes commands.

What is Laravel command in PHP?

Laravel Commands. Laravel command is the most popular and widely used PHP framework which is based on MVC (Model View Controller) Architecture. It is an open source web application development framework and was created by Taylor Otwell.


2 Answers

Actually figured this out. Further down the documentation it states that you must register your command in "app/start/artisan.php" using the following method:

Artisan::add(new import);

Also the name you give in your command class is significant as that's what you need to use to call it. So I should have actually been calling it like so:

php artisan command:import

One final thing. What the fire() returns is unimportant, to return strings you must echo them.

like image 183
John Mellor Avatar answered Oct 26 '22 01:10

John Mellor


try this.

protected function getArguments()
{
    return [];
}

protected function getOptions()
{
    return [];
} 

also add this in /app/start/artisan.php

Artisan::add(new ParseCommand);

then Run Command on Root directory

./artisan command:import; 
like image 32
user3228017 Avatar answered Oct 26 '22 02:10

user3228017