Home > Backend Development > PHP Tutorial > Example of running PHP code using cli command in thinkPHP

Example of running PHP code using cli command in thinkPHP

小云云
Release: 2023-03-21 06:30:01
Original
4778 people have browsed it

In the process of working on projects these days, I happened to need to use cli in thinkPHP to run a method in a controller. I searched it in the official manual. After I found it, I didn’t know how to use it at all. This article is mainly about Let's share an example of running PHP code using the cli command in thinkPHP. I hope it can help you.



I am confused. I have been confused about how to use cli to execute methods under the controller in Baidu tp5. Even I want to , directly run the PHP file, I instantiate the class in the class file that needs to be executed and call the method, and then enter the directory where the file is located in the cmd command

Just, if you execute a single PHP script, there is no problem with this approach, but if you want to run the files in the tp5 framework, various classes and usage methods will not be found or defined.

Later I thought, since I couldn’t find the method under the execution controller, then I would put the code in the execute method of the command file and execute it directly,


Then create a new command folder in the index module, and create a new Test.php file in the folder. The code is as follows:



In the execute method, place the PHP code that needs to be run. You can output the results you want in the left back.

Then enter the project directory on the cmd command line and enter


You can run the PHP code and output the results! Finally solved it, now the problem I encounter is how to operate the thinkPHP cli command line in Linux!

Related recommendations:

VUE-CLI @2.9.1 and later versions

vue-cli is simple to develop multi-page applications Example

Introduction to the method of passing custom parameters to the cli command in php

The above is the detailed content of Example of running PHP code using cli command in thinkPHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template