Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mohamedhassan1's avatar

how call controller from a command line ?

hi to everyone , i want to make a job that run code every hour so i want to call controller from command line ? can anyone understand me how to do this ? -- thanks>>

0 likes
11 replies
mohamedhassan1's avatar

Thanks for replaying , but i think this n't make what i want ?! > OR can Y tell me how do this as i put controller class in schedule function in kernal ?

RachidLaasri's avatar

You need to extract that logic to its own class and call it from your controller & schedule.

2 likes
pmall's avatar

Create a job which is run by the controller and by the command.

1 like
jekinney's avatar

Yes you can, but the prefers method is call a class from the commands file. If you really want to call a controller (which is just a PHP class anyways) just add it in.

1 like
mohamedhassan1's avatar

Can anyone make a simple code that make a job run a controller every hour ?!

pmall's avatar
pmall
Best Answer
Level 56

Again, it is a bad idea to "run" a controller.

Create a service class (a bare class) that do what you want.

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();
    }
}
3 likes
mohamedhassan1's avatar

pmall ,Y see this not a good idea ! i want to make scrapper so i want the code of scrapping running everyhour ! do Y think the better using server to run the file "scrapping file" directly by writing a crone job command or using the code Y write ?! thanks for your help ☺

pmall's avatar

i dont know it is up to you to choose how you want to scrap

1 like
GimmeMylanta's avatar

@pmall would your answer to this question be classified as a 'service container' now in in L5.3?

alinad's avatar

how i can to call the funtion in this handle function

public function handle() {

}

or from kernel file

protected function schedule(Schedule $schedule) {

}

Please or to participate in this conversation.