abraham foto
abraham foto

Reputation: 457

laravel: scheduler cant find my controller

I created a new class in the command.in the folder App\Console\Commands;

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'schadular';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
         return $schedule->call('Schedular@schedule')->everyMinute();
    }
}

and in the kern.php i wrote the following code

<?php

namespace App\Console;

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use \App\Controllers;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        'App\Console\Commands\Schedular'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

       return $schedule->call('')->everyMinute();
    }

    /**
     * Register the Closure based commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        require base_path('routes/console.php');
    }
}

am i supposed to write the controller class here? or what should i write?

 return $schedule->call('')->everyMinute();

or should i create new class which has the same functionality as the controller?

Upvotes: 1

Views: 123

Answers (1)

sepehr
sepehr

Reputation: 18475

You should consider controllers as just a transport layer for your application. Any actual functionality logic should go into your domain classes.

Try refactoring your controller functionality in a way that it consumes, for example, a service class. Then make a command class that utilizes the same service class as well, providing another way to trigger the functionality.

This way, you have the functionality encapsulated in a service class. Both controller and command classes are just consuming that class as just transport means; HTTP requests and CLI commands.

When done with refactoring, you can schedule your command to be run every minute.

Upvotes: 2

Related Questions