I have a couple of classes (services) which I use in the main Laravel app as well as via artisan
commands.
What I would like to do is being able to display some output when invoking such class methods via artisan
(as $this->info(...)
inside the command itself) and just eventually return a value (response, result object or similar) when using them within the main app.
Right now I just used them as commands and achieved the output effect by echo
ing the statements I needed directly from within the methods, but I'm convinced this is not the correct approach because as soon as I start using them in the app I guess I'm gonna have my responses polluted with these info statements (as well as double headers or similar issues, I imagine).
I wonder what would be a better way to approach such scenario.
Below a code example of what I'm trying to achieve:
// DoSequenceCommand
public function fire() {
MyClass::doSequence();
}
// MyClass (Facade)
public function doSequence() {
echo "Beginning Step 1\n"; // THIS I WOULD BE ABLE TO OUTPUT ONLY IF EXECUTED VIA COMMAND LINE
$s1 = $this->step1();
echo "Step 1 " . ( $s1 ? "succeded" : "failed" ) . "!\n";
echo "Beginning Step 2\n";
$s2 = $this->step2();
echo "Step 2 " . ( $s2 ? "succeded" : "failed" ) . "!\n";
return [ "s1" => $s1, "s2" => $s2];
}
You could either:
doSequence()
to determine if it should output info statements doSequenence()
and listen for them in your command to output info from there The events option might be the cleanest way.
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.