ThinkPHP5命令⾏执⾏控制器⽅法! 平时在开发的时候为了⽅便定时任务执⾏某些⽅法,我们可以通过tp的⾃定义命令⾏来实现 ⾸先创建⼀个 application/common/command/Action.php
<?php
/**
邮礼网* Created by PhpStorm.
* User: Administrator
* Date: 2019/12/23
* Time: 12:35
*/
namespace app\common\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Action extends Command
{
protected function configure()
{
n80
$this->setName('action')
->addArgument('route', Argument::OPTIONAL, "your run route path! ")//路由地址必须输⼊ ->addOption('option', 'o', Option::VALUE_REQUIRED, 'set Controller Argument')//参数选填
宪政经济学-
>setDescription('Command run Controller Action!');
}
protected function execute(Input $input, Output $output)
{
$Argument = $input->getArguments();人体
气化是什么意思
if ($Argument['command'] == 'action') {
if ($input->hasOption('option')) {
$result = action($this->route($Argument['route']), $input->getOption('option'));
$output->writeln($result);
} else {
$result = action($this->route($Argument['route']));
$output->writeln($result);
}
}
}
public function route($route = '')
otcbb{
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;
}
}
然后在 application/command.php 添加
return [
'app\common\command\Action',
];
政如农工
最后执⾏ php think
我们创建 application/index/controller/Test.php 并添加如下内容namespace app\index\controller;
use think\Controller;
/**
* 前台⾸页控制器
*
* @package app\index\controller
*/
class Test extends Controller
{
public function test($a = '')
{
return 'Test Commant :' . $a;
}
最后我们来测试⼀下输⼊如下命令
>php think action index/test/test -o a=1