Artisan Command Calling Controller

0

I made a function in a Controller and created a Command in Artisan to run this function.

Follows:

Controller

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\EmpresaRating;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CronController extends Controller
{
    public static function fireCron(){
        $ratings = EmpresaRating::where('quarentena', '=', 1)
        ->whereRaw("DATE_FORMAT(data_publicacao, '%Y-%m-%d') = '".date('Y-m-d')."'");

        if($ratings->exists()){
            $ratings->update(['quarentena' => 0]);
        }
    }
}

CronCommand

<?php

namespace App\Console\Commands;

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

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cron para atualizar avaliações que estão em quarentena após 7 dias.';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $controller = app()->make('App\Http\Controllers\CronController');
        app()->call([$controller, 'fireCron'], []);
    }
}

Kernel

protected $commands = [
    \App\Console\Commands\Inspire::class,
    \App\Console\Commands\CronCommand::class,
];

When I run php artisan cron_services it gives me this error:

  

[Symfony \ Component \ Debug \ Exception \ FatalErrorException] Call to a   member function getPath () on null

What am I missing?

    
asked by anonymous 06.01.2017 / 14:40

1 answer

0

It seems that for some reason Command was not accessing my Controller. So I did it using Service Provider .

I created a provider called CronProvider:

CronProvider

public function boot(){

}

public function register(){
   $this->app->singleton('App\Cron\Cron', function ($app) {
       return new Cron;
   });
}

public function provides(){
   return ['App\Cron\Cron'];
}

I created a folder called Cron and created a file called Cron.php inside it.

Cron

public static function isDeferred(){
   return true;
}

public function provides(){
   return [
      'App\Cron\Cron'
   ];
}

public function fireCron(){
   $ratings = EmpresaRating::where('quarentena', '=', 1)
   ->whereRaw("DATE_FORMAT(data_publicacao, '%Y-%m-%d') = '".date('Y-m-d')."'");

   if($ratings->exists()){
       $ratings->update(['quarentena' => 0]);
   }
}

Then I created a command called CronCommand , not forgetting use App\Cron\Cron to call my class .

CronCommand

protected $signature = 'cron:services';

protected $description = '7 dias.';

use App\Cron\Cron;

public function __construct()    {
   parent::__construct();
}

public function handle(Cron $cron){
   $cron->fireCron();
}

I registered in the% com_of% of the Commands folder.

protected $commands = [
    \App\Console\Commands\Inspire::class, 
    \App\Console\Commands\CronCommand::class
];

I registered the file Kernel.php of the config folder.

App\Providers\CronProvider::class,

Then I ran the command:

php artisan cron:services

It worked!

    
06.01.2017 / 19:25