Laravel7.x 命令:如何从命令文件的 handle() 方法路由到 url?

发布于 2025-01-18 05:32:25 字数 1516 浏览 0 评论 0原文

如何从Laravel的App/Console/commands/mycommand.php的 handler()方法的参数路由到另一个URL?

我想用handle()定制命令文件的参数(路由)将(路由)拨打控制器的句柄()的参数()的参数(),然后通过route()通过route()将是handle()传递的参数()将是handle()在控制器中。

app/console/commands/myCommand.php

public function handle()
    {
        $sale_days = SaleDay::latest()->first();
        Log::info('test command file');

        $latest_date = $sale_days->date;
        // $request = Request::create('/game/end/day/'.$latest_date, 'get');
        // '<a href=' . route('game.endday', $latest_date) . '></a>';
        // return $request;
        return redirect()->to('game.endday', $latest_date);
    }
web.php

    Route::get('/game/end/day/{date}', 'SaleDayController@endday')->name('game.endday');
controller 

public function endday($date) {

    // $date = \Artisan::call('auto_day:close');
    Log::info("command endday module in saledaycontroller");
    $new_Date=Carbon::create($date);
    $prev_sale_day=$this->daySaleService->endDay($date);
    
    $new_sale_day=$this->daySaleService->firstOrCreate($new_Date->addDays(1));
    $this->daySaleService->changeCurrentDay($new_sale_day->id);
    $this->daySaleService->shiftDayGameSales($new_sale_day->id,$prev_sale_day->id);
    Log::info("command endday module in saledaycontroller ENDDDDD");

    return redirect()->route('game.sale');
}

句柄函数无法正常工作,如何从handing()方法路由使用参数路由

How to route to another url with a parameter from laravel's app/console/commands/myCommand.php's handler() method?

I want to route to another url/page/route with a parameter from handle() method of custom made command file which(route) later will call a controller and that parameter passed by handle() via route() will be handle() there in controller.

app/console/commands/myCommand.php

public function handle()
    {
        $sale_days = SaleDay::latest()->first();
        Log::info('test command file');

        $latest_date = $sale_days->date;
        // $request = Request::create('/game/end/day/'.$latest_date, 'get');
        // '<a href=' . route('game.endday', $latest_date) . '></a>';
        // return $request;
        return redirect()->to('game.endday', $latest_date);
    }
web.php

    Route::get('/game/end/day/{date}', 'SaleDayController@endday')->name('game.endday');
controller 

public function endday($date) {

    // $date = \Artisan::call('auto_day:close');
    Log::info("command endday module in saledaycontroller");
    $new_Date=Carbon::create($date);
    $prev_sale_day=$this->daySaleService->endDay($date);
    
    $new_sale_day=$this->daySaleService->firstOrCreate($new_Date->addDays(1));
    $this->daySaleService->changeCurrentDay($new_sale_day->id);
    $this->daySaleService->shiftDayGameSales($new_sale_day->id,$prev_sale_day->id);
    Log::info("command endday module in saledaycontroller ENDDDDD");

    return redirect()->route('game.sale');
}

the handle function is not working properly, How can I route from handle() method with a parameter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2025-01-25 05:32:25

这是一个可怕的主意。最好将您的控制器中的代码抽象到服务中,然后在命令和控制器中调用该服务。

或者,或者在控制器和命令中触发事件,并为该事件的侦听器执行您的逻辑。

This is a terrible idea. You would be better served either abstracting the code in your controller to the service and then calling that service in both the command and the controller.

Or alternatively trigger an event in the controller and command, and have a listener for that event that executes your logic.

旧人九事 2025-01-25 05:32:25

也许你可以尝试一下

app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle(YourRequest);

Maybe you can try this

app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle(YourRequest);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文