您现在的位置是:网站首页>软件开发>开发终端>thinkphpthinkphp

linux下执行tp内的方法如何实现---命令行

风口下的猪2021-12-09thinkphp

简介

项目经常会遇到定时任务、进程守护等需求,另外通过tp编写的一些方法不希望被外部URL调用,仅linux执行。

此时就会用到命令行,命令行的作用就是拿来linux通过php 指令进行执行。

它的好处是不像linux直接执行类->方法有时会出现第三方类库not found的情况。

  

第一步,创建一个自定义命令类文件

cd到项目文件根目录,运行指令

php think make:command Hello hello

会生成一个app\command\Hello命令行指令类,我们修改内容如下:

<?php
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class Hello extends Command
{
    protected function configure()
    {
        $this->setName('hello')
        ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
        ->setDescription('Say Hello');
    }
    protected function execute(Input $input, Output $output)
    {
    $name = trim($input->getArgument('name'));
      $name = $name ?: 'thinkphp';
if ($input->hasOption('city')) {
        $city = PHP_EOL . 'From ' . $input->getOption('city');
        } else {
        $city = '';
        }
        
        $output->writeln("Hello," . $name . '!' . $city);
    }
}

这个文件定义了一个叫hello的命令,并设置了一个name参数和一个city选项。


第二步,配置config/console.php文件

<?php
return [
    'commands' => [
        'hello' => 'app\command\Hello',
    ]
];


第三步,测试-命令帮助-命令行下运行

php think

输出

Think Console version 0.1
Usage:
  command [options] [arguments]
Options:
  -h, --help            Display this help message
  -V, --version         Display this console version
  -q, --quiet           Do not output any message
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
  build              Build Application Dirs
  clear              Clear runtime file
  hello              Say Hello 
  help               Displays help for a command
  list               Lists commands
 make
  make:controller    Create a new resource controller class
  make:model         Create a new model class
 optimize
  optimize:autoload  Optimizes PSR0 and PSR4 packages to be loaded with classmaps too, good for production.
  optimize:config    Build config and common file cache.
  optimize:schema    Build database schema cache.


第四步,运行hello命令

cd到项目根目录

php think hello

输出

Hello thinkphp!


添加命令参数

php think hello kancloud

输出

Hello kancloud!


添加city选项

php think hello kancloud --city shanghai

输出

Hello kancloud!

From shanghai




很赞哦! (0)

  • 软件开发
  • 素质要求
  • 计算机基础
  • 架构
  • 安全
  • 性能
  • 运维
  • 尾页
  • 数据库
  • 开发终端
  • 语言基础
  • 项目管理
  • 产品设计
  • 系统
  • 工作规范
  • 计算机网络
  • 前端技术栈
  • 数据结构
  • 计算机组成原理
  • 后端技术栈
  • 性能优化
  • 安全设计
  • 常见模块
  • 计算机操作系统
  • 服务器
  • python
  • MySQL
  • thinkphp
  • PHP
  • Java
  • JavaScript
  • Windows
  • Linux
  • 特效
  • indexedDB
  • vue
  • 淘宝联盟
  • Ionic
  • Angular
  • 微信小程序
  • 支付宝小程序
  • uni-app
  • css/sass/less
  • 支付
  • socket
  • 爬虫
  • web性能优化
  • 消息推送
  • CVM
  • sqlite
  • Redis
  • 前端基础
  • 基础
  • element
  • Nginx
  • yii2
  • /ponder/index.php/index/catelist/catelist/cateid/10.html

    相关阅读 (同一栏目)

    << /

    标签云

    站点信息

    • 文章统计:528篇
    • 移动端访问:扫码进入SQ3R