namespace App\Services;
class FooService
{
public function performFoo ()
{
// Do what you need
}
}
Then inject it in the controller actions you want and use it :
namespace App\Http\Controllers;
// ...
use App\Services\FooService;
class SomeController extends Controller
{
public function SomeAction (FooService $foo_service)
{
$foo_service->performFoo();
}
}
Then create a command and inject the service to use it :
namespace App\Console\Commands;
// ...
use App\Services\FooService;
class Foo extends Command
{
protected $signature = 'foo';
protected $description = 'Perform foo';
private $foo_service;
public function __construct(FooService $foo_service)
{
$this->foo_service = $foo_service;
}
public function handle ()
{
$this->foo_service->performFoo();
}
}
Then in app/Console/Kernel.php set the scheduling :
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
\App\Console\Commands\Inspire::class,
\App\Console\Commands\Foo::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('foo')->hourly();
}
}