简体   繁体   中英

Laravel artisan command line

I am using Laravel 4.1 and the artisan tool to develop some cli scripts for our website. These scripts will run in the background though cron jobs.

I have come across a bug/feature and was wondering if anyone else has run into this problem or knows how to overcome this.

Description:

In the start folder under artisan.php i have added my commands:

Artisan::add(new VinCommand);
Artisan::add(new DealersCommand);
Artisan::add(new ReportingCommand);
Artisan::add(new AutoInventoryCommand);
Artisan::add(new VastLeadsCommand);
Artisan::add(new SendInventoryAlertsCommand);
Artisan::add(new JumpstartCapCommand); 

Under app -> commands i have all my commands. In AutoInventoryCommand.php i am running a truncate table in the __construct method, but i am finding that the truncate is being called on every cli command.

For instance VinCommand:

php artisan command:vin_command

also runs AutoInventoryCommands __construct method, which is causing the table to always be truncated.

Is it common knowledge not to put any logic in the __construct method?

In some of the other commands i set private static variables in the __contruct method. Is this bad practice. Should i have an _init() method that gets called in:

public function fire()
{
    $this->_init();
}

And this will set my variables. I would have never have caught this if i didnt have an:

echo "Table Truncated \n"; 

in the truncate method.

So is this a bug/common knowledge

I assume this is happening because all the __constructs are called when the:

Artisan::add();

is called?

Of course it is run on every command. You have new AutoInventoryCommand right there in your code!

If you insist on keeping your code in the constructor (which you shouldn't), you can register your commands with resolve instead:

Artisan::resolve('AutoInventoryCommand');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM