tp5.1
-
tp5实现自定义命令
步骤
第一步,创建一个自定义命令类文件,新建 application/common/command/Hello.php
<?php 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 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 选项。
第二步,配置 application/command.php 文件
<?php
return [
'app\common\command\Hello',
];
第三步,测试-命令帮助-命令行下运行
php think
tips:
think是指当前项目thinkphp目录,所以如果脚本在项目根目录那么可以直接使用think,否则需要使用实际路径才行
比如: echo exec("php “.ROOT_PATH.”/think message {KaTeX parse error: Expected 'EOF', got '}' at position 7: openid}̲ {app_id} "))
message是配置好的命令行脚本类名,后面是传入的参数,多个参数空格间隔
输出
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 f
or 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 to
o, good for production.
optimize:config Build config and common file cache.
optimize:schema Build database schema cache.
第四步,运行 hello 命令
php think hello
输出
Hello thinkphp!
自定义指令
添加命令参数
php think hello liuchen
输出
Hello liuchen!
添加 city 选项
php think hello liuchen --city shanghai
输出
Hello thinkphp!
From shanghai
注意看参数和选项的调用区别
在控制器中调用命令
支持在控制器的操作方法中直接调用命令,例如:
<?php
namespace app\index\controller;
use think\Console;
use think\Controller;
class Index extends Controller
{
public function hello($name)
{
$output = Console::call('hello ' . $name);
return $output->fetch();
}
}
访问该操作方法后,例如:
http://tp5.com/index/index/hello/name/thinkphp
页面会输出
Hello thinkphp!
一旦在控制器中调用命令,并不会生成cli命令行日志,而是在普通的web日志中记录。
总结:
1、调用可以采用system()或者exec()方法,命令格式为(tp5) php think message
这里的message就是新增的命令名称,在command中定义的
还有php是使用服务器默认的php解析器(php -v可以查看当前是哪个php,如果存在多个版本),think是指当前项目thinkphp目录,所以如果脚本在项目根目录那么可以直接使用think,否则需要使用实际路径才行
比如: echo exec("php “.ROOT_PATH.”/think message {KaTeX parse error: Expected 'EOF', got '}' at position 7: openid}̲ {app_id} ");
这里message后面的参数是需要传入的参数,需要在对应的command目录定义的类进行解析
2、注意使用命令行执行脚本的时候不要使用var_dump()输出,因为多个var_dumo只会显示最后一个,并且命令行模式不能够直接输出数组等特殊格式,需要转换成字符串。
-
tp5框架搭建
1、下载tp5框架文件,直接把目录上传到服务器对应目录下,并且检查目录权限
tp5虽然可以直接使用,但是往往搭建过程容易出现各种错误,其中目录权限也是一个很频繁的问题,如果报错出现访问不了的情况,先检查public 目录是否有访问权限,以及当前用户是否对项目文件有读写权限
常见问题:
1)public下普通文件可以访问,但是无法路由到对应的控制器显示主页
答:很有可能是没有访问权限导致的,也就是不能跨目录,只有当前目录有权限,可以在index.php入口文件中打开php错误调试,直接用error_reporting开启所有错误,就可以在浏览器查看错误。如果使用宝塔的情况,上传到宝塔上会自动生成一个user.ini文件,该文件会导致无法跨目录访问,所以有时候会出现报错500,显示不了页面,但是错误日志里却没有体现,这种情况就需要断点法从入口开始调试才能发现问题
2)还有一种情况就是访问不了PHP文件
这种情况是因为服务器nginx或者apache站点配置对php后缀文件没有支持,在conf文件或者vhosts下站点配置文件新增index.php的支持接口3)有时候会出现目录权限正常,也可以访问php文件,但是还是访问不了对应控制器方法。
很有可能是没有使用重写规则,所有的框架都有一定的路由规则,通过入口文件路由到对应控制器,那么重写规则就是让url可以更加简洁准确的路由到正确的方法中去,通常的重写规则会隐藏掉入口文件index.php
以nginx为例,搭建tp5,如果nginx版本比较低,可能就不支持pathinfo,但是可以通过在 Nginx.conf 中配置转发规则实现:location / { // …..省略部分代码 if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } }
其实内部是转发到了ThinkPHP提供的兼容URL,利用这种方式,可以解决其他不支持PATHINFO的
WEB服务器环境。
如果你的应用安装在二级目录, Nginx 的伪静态方法设置如下,其中 youdomain 是所在的目录名称。
URL访问location /youdomain/ { if (!-e $request_filename){ rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=/$1 last; } }
原来的访问URL:
http://serverName/index.php/模块/控制器/操作/[参数名/参数值...]
设置后,我们可以采用下面的方式访问:
http://serverName/模块/控制器/操作/[参数名/参数值...]
-
33
-
33
-
44
-
55