我正在使用 Artisan
通过命令行调用 Laravel 5 Route
。我按照这篇文章中的说明创建了一个命令行 Controller :Call laravel controller via command line .
当我在网络浏览器中导航到这条路线时,我没有收到任何错误。此路由执行 API 调用,并使用我创建的名为 Ticker
的 Laravel
Model
将数据插入 MySQL 数据库。
当我可以从 CLI 调用我的路由时,如下所示:
$ php artisan route:call /d17059dfce4c09ef5e437b1d9455f7c6
我收到一个 Laravel
错误(其中 truncate 'tickers'
是在路由调用的 Controller 中执行的第一个 SQL 命令,而 tickers
是数据库中的表),说明:
SQLSTATE[HY000] [2002] No such file or directory (SQL: truncate `tickers`)
因为我似乎只是在通过命令行执行此路由时才收到错误,这让我相信命令行脚本 App\Console\Commands\CallRoute.php 一定有问题
看起来像这样:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Http\Requests;
class CallRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:call {uri}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'php artsian route:call /route';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$request = Request::create($this->argument('uri'), 'GET');
$this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
}
}
如能提供故障排除或解决此错误的任何帮助,我们将不胜感激。提前谢谢你。
最佳答案
您应该将 /d17059dfce4c09ef5e437b1d9455f7c6
作为选项而不是参数传入。它将其解释为一个目录。
php artisan route:call --uri=/d17059dfce4c09ef5e437b1d9455f7c6
更新命令签名以包含 uri
选项:
protected $signature = 'route:call {--uri=}';
命令:
class CallRoute extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'route:call {--uri=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'php artsian route:call /route';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$request = Request::create($this->option('uri'), 'GET');
$this->info(app()->make(\Illuminate\Contracts\Http\Kernel::class)->handle($request));
}
}
关于php - Laravel artisan 路线 :call Throws MySQL Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49713906/