3

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!

1 Answer 1

4

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.