laracasts / Commander

Easily leverage commands and domain events in your Laravel projects.
https://laracasts.com/series/commands-and-domain-events
MIT License
279 stars 68 forks source link

Unable to use $this->execute inside an artisan command #23

Closed lucacri closed 10 years ago

lucacri commented 10 years ago

Hi, i'm trying to use the CommanderTrait inside an artisan command, but i receive the following error: {"error":{"type":"ErrorException","message":"Declaration of Laracasts\\Commander\\CommanderTrait::execute() should be compatible with Illuminate\\Console\\Command::execute(Symfony\\Component\\Console\\Input\\InputInterface $input, Symfony\\Component\\Console\\Output\\OutputInterface $output)","file":"\/Users\/lucacri\/Sites\/YodaImporter\/app\/commands\/ReassignVncPorts.php","line":77}}

From what i can see, the Illuminate\Console\Command has already an execute method in line 110: protected function execute(InputInterface $input, OutputInterface $output)

and it's not working well with the CommanderTrait. Any suggestion on how to fix it?

laracasts commented 10 years ago

That's because the command class you're extending already has an execute method. So, when you pull in the trait, they're clashing.

lucacri commented 10 years ago

I know, I figured it out after a bit of head-scratching :-)

I know it would break backwards-compatibility to change the method name, so I guess we can either close it or maybe create a wrapper trait for artisan commands. I used this package in all my CLI commands and it makes a lot of sense (CLI and Web are both external facing parts of the app). What do you think?

abbajbryant commented 10 years ago

Maybe a more explicit executeCommand function name would be more semantic anyways?

samwalshnz commented 10 years ago

+1 - It wouldn't be backwards compatible, but I also think 'executeCommand' would be a better method name

samwalshnz commented 10 years ago

Does anyone know of a way around it without modifying the CommanderTrait?

stevenklar commented 10 years ago

@samwalshnz @laracasts @lucacri

http://www.sitepoint.com/using-traits-in-php-5-4/

Use the alias.

use Commander {
    Commander::execute as executeCommand
}

(its bad, but if you want to avoid extend/modify the existing commander, its a solution)

samwalshnz commented 10 years ago

Oh man! You learn something every day. Thank you @stevenklar!

bruno-barros commented 10 years ago

wow. Really nice reference.

arthurkirkosa commented 10 years ago

@stevenklar ... I'm still getting the same error. Can you paste a code that works?

juukie commented 10 years ago

use Commander { executeCommand insteadof Commander::execute } should be possible too (according to the link provided above).

mdurao commented 10 years ago

Unable to use the alias also...

samwalshnz commented 10 years ago

@JeffreyWay, not sure if this means this issue should be reopened or not, but I've just tried to use the code examples @juukie14 and @stevenklar have recommended with no success.

cmgmyr commented 10 years ago

I am having the same issue with the conflicts too

ghost commented 9 years ago

Has anyone solved this?

ghost commented 9 years ago

See https://laracasts.com/discuss/channels/general-discussion/laracasts-commander-with-artisan-commands

I used the answer given there but don't use the Trait.