fastadmin命令行command执行控制器方法
在fastadmin中使用命令行调试接口
1、找到command目录,fastadmin在admin/command,在command目录下新建action.php文件,action则为命令名称(之后执行的时候这样:php think action),内容如下:
action.php内容如下:
<?php
namespace app\admin\command;
use app\admin\command\Api\library\Builder;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\input\Argument;
use think\console\Output;
use think\Exception;
class Action extends Command
{
protected function configure()
{
$this
->setName('action') // 命令的名字
->addArgument('api', Argument::OPTIONAL, '控制器方法名 (api/index/index)', 'api/index/index')
->addOption('option', 'o', Option::VALUE_REQUIRED, 'set controller action argument, format:a=1,b=2') // 配置参数,支持多个参数
->setDescription('命令行执行接口') //运行 "php think list" 时的简短描述
->setHelp("执行接口 参数为api (api/index/index)"); // 运行命令时使用"--help"选项时的描述
}
protected function execute(Input $input, Output $output)
{
$Argument = $input->getArguments();
if ($Argument['command'] == 'action') {
if ($input->hasOption('option')) {
$result = action($this->route($Argument['api']), $this->option($input->getOption('option')));
$output->writeln($result);
} else {
$result = action($this->route($Argument['api']));
$output->writeln($result);
}
}
}
public function route($route = '')
{
if ($route) {
$route = explode('/', $route);
$module = isset($route[0]) ? $route[0] : 'index';
$controller = isset($route[1]) ? $route[1] : 'index';
$action = isset($route[2]) ? $route[2] : 'index';
return $module . '/' . $controller . '/' . $action;
}
return $route;
}
public function option($option = ''){
/* 整理成数组 start */
$params = array();
$option_arr = explode(',', $option);
foreach ($option_arr as $key => $val){
$tmp_params = explode('=', $val);
$params[$tmp_params[0]] = $tmp_params[1];
}
/* 整理成数组 end */
return $params;
}
}
<?php
namespace app\admin\command;
use app\admin\command\Api\library\Builder;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\input\Argument;
use think\console\Output;
use think\Exception;
class Action extends Command
{
protected function configure()
{
$this
->setName('action') // 命令的名字
->addArgument('api', Argument::OPTIONAL, '控制器方法名 (api/index/index)', 'api/index/index')
->addOption('option', 'o', Option::VALUE_REQUIRED, 'set controller action argument, format:a=1,b=2') // 配置参数,支持多个参数
->setDescription('命令行执行接口') //运行 "php think list" 时的简短描述
->setHelp("执行接口 参数为api (api/index/index)"); // 运行命令时使用"--help"选项时的描述
}
protected function execute(Input $input, Output $output)
{
$Argument = $input->getArguments();
if ($Argument['command'] == 'action') {
if ($input->hasOption('option')) {
$result = action($this->route($Argument['api']), $this->option($input->getOption('option')));
$output->writeln($result);
} else {
$result = action($this->route($Argument['api']));
$output->writeln($result);
}
}
}
public function route($route = '')
{
if ($route) {
$route = explode('/', $route);
$module = isset($route[0]) ? $route[0] : 'index';
$controller = isset($route[1]) ? $route[1] : 'index';
$action = isset($route[2]) ? $route[2] : 'index';
return $module . '/' . $controller . '/' . $action;
}
return $route;
}
public function option($option = ''){
/* 整理成数组 start */
$params = array();
$option_arr = explode(',', $option);
foreach ($option_arr as $key => $val){
$tmp_params = explode('=', $val);
$params[$tmp_params[0]] = $tmp_params[1];
}
/* 整理成数组 end */
return $params;
}
}
<?php namespace app\admin\command; use app\admin\command\Api\library\Builder; use think\Config; use think\console\Command; use think\console\Input; use think\console\input\Option; use think\console\input\Argument; use think\console\Output; use think\Exception; class Action extends Command { protected function configure() { $this ->setName('action') // 命令的名字 ->addArgument('api', Argument::OPTIONAL, '控制器方法名 (api/index/index)', 'api/index/index') ->addOption('option', 'o', Option::VALUE_REQUIRED, 'set controller action argument, format:a=1,b=2') // 配置参数,支持多个参数 ->setDescription('命令行执行接口') //运行 "php think list" 时的简短描述 ->setHelp("执行接口 参数为api (api/index/index)"); // 运行命令时使用"--help"选项时的描述 } protected function execute(Input $input, Output $output) { $Argument = $input->getArguments(); if ($Argument['command'] == 'action') { if ($input->hasOption('option')) { $result = action($this->route($Argument['api']), $this->option($input->getOption('option'))); $output->writeln($result); } else { $result = action($this->route($Argument['api'])); $output->writeln($result); } } } public function route($route = '') { if ($route) { $route = explode('/', $route); $module = isset($route[0]) ? $route[0] : 'index'; $controller = isset($route[1]) ? $route[1] : 'index'; $action = isset($route[2]) ? $route[2] : 'index'; return $module . '/' . $controller . '/' . $action; } return $route; } public function option($option = ''){ /* 整理成数组 start */ $params = array(); $option_arr = explode(',', $option); foreach ($option_arr as $key => $val){ $tmp_params = explode('=', $val); $params[$tmp_params[0]] = $tmp_params[1]; } /* 整理成数组 end */ return $params; } }
2、在command.php文件内引入命令
'app\admin\command\action'
3、检查是否引入成功此命令
php think
检查Available commands 列表里出现此命令代表引入成功
4、执行命令
php think action api/index/index -o data1=1,data2=2
也可以直接执行命令:
php public/index.php 控制器方法名称
例:php public/index.php api/index/index
有错误或不足之处敬请纠正,谢谢!
版权声明:
作者:admin
链接:http://blog.mryxh.cn/3580.html
文章版权归作者所有,未经允许请勿转载。
THE END

0

打赏

分享

二维码

海报