ThinkPHP5命令行执行控制器方法!

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

本文发布于:2024-09-22 07:22:17,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/153163.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:命令   控制器   气化   时候   路由   选填
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议