Miril Terolli
Miril Terolli

Reputation: 221

Laravel Run function from console command

LARAVEL 5.2, just created the command named "HelloWorld" and here is the code:

 <?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\HelloWorldController;

class MakeImportsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'helloworld';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Say Hello World Controller';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        return $this -> helloWorld();

    }
}

My controller HelloWorldController.php looks as below:

    <?php

namespace App\Http\Controllers;

class HelloWorldController extends Controller
{
    public function helloWorld() {
        echo 'Hello World from controller';
    }

}

My Kernel.php has following commands so far:

protected $commands = [
        Commands\Inspire::class,
        Commands\HelloWorldCommand::class,
    ];

When I run the controller VIA Routing method it works, but I want to run this via Console command. Here is my command on console : php artisan helloworld . And I get the error :

 [Symfony\Component\Debug\Exception\FatalErrorException]Call to undefined method App\Console\Commands\HelloWorldCommand::helloWorld()

My question is: Is there any way to call this function VIA command console? How? Thank you in advance!

Upvotes: 3

Views: 7917

Answers (1)

Miril Terolli
Miril Terolli

Reputation: 221

Solved! I've just placed on handle controller's class name and called the function as following:

$x = new HelloWorldController(); 
echo $x->helloWorld();

It worked!

Upvotes: 4

Related Questions