Php 通过artisan命令调用时请求参数丢失
在artisan命令中,使用参数创建请求以调用路由时,目标路由不接收请求参数 testcommand:Php 通过artisan命令调用时请求参数丢失,php,laravel,lumen,laravel-artisan,Php,Laravel,Lumen,Laravel Artisan,在artisan命令中,使用参数创建请求以调用路由时,目标路由不接收请求参数 testcommand: <?php namespace App\Console\Commands; use Illuminate\Console\Command; use Laravel\Lumen\Http\Request; class TestCommand extends Command { /** * The name and signature of the console command.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Laravel\Lumen\Http\Request;
class TestCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$date = [
'username' => 'test',
];
$tokenRequest = Request::create('/test', 'post', $date);
$res = app()->handle($tokenRequest)->withHeaders(['Content-Type' => 'application/json']);
$responseBody = json_decode($res->getContent());
var_dump($responseBody);
}
}
另一个问题是管腔的缺陷。
当我们运行php artisan route:run in terminal时,目标路由没有包含参数。第一个问题,为什么要在命令中发送请求?
php artisan
和Lumen?您是在混合Laravel和Lumen吗?@lagbox我们需要在pub/sub、命令订阅频道中使用参数调用路由,并且在接收数据时需要调用路由并将参数传递给该路由。您为什么需要调用路由?你不能从控制器中重构代码并直接调用它吗?您可以将复制的代码重构成另一个命令或动作类,并直接从控制器和命令调用它。
<?php
namespace App\Http\Controllers;
use Laravel\Lumen\Http\Request;
class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function test(Request $request)
{
$this->validate($request,[
'username'=>'required'
]);
return $request->all();
}
}
$router->post('/test','ExampleController@test');