Laravel7.x 命令:如何从命令文件的 handle() 方法路由到 url?
如何从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个可怕的主意。最好将您的控制器中的代码抽象到服务中,然后在命令和控制器中调用该服务。
或者,或者在控制器和命令中触发事件,并为该事件的侦听器执行您的逻辑。
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.
也许你可以尝试一下
Maybe you can try this