What is the CLI:
The CLI (command line interface) is a text-based and computer-interactive approach. For more information, please see wikipedia .
Why use the CLI?
Although not obvious, there are a number of situations where we need to run CodeIgniter using the command line.
- Use Cron to run tasks at timed intervals without using wget or curl
- Use the return value of the function is_cli () to make your cron page inaccessible through the URL
- Create interactive tasks such as: Set permissions, clear cache, backup, etc.
- Integrate with other languages, such as the ability to invoke a directive in C + + to run code in your model.
Example:
Let's first create a simple controller, open your text editor, create a new file and name it tools.php, then enter the following code:
<? PHP class extends Ci_controller { publicfunction message ($to = ' World ') { Echo "Hello {$to}!". Php_eol ; }}
Then save the file to the application/controllers/directory.
Now you can access it by using a URL similar to the following:example. COM/index. php/Tools/message/ to
Alternatively, we can access it through the CLI. Under Mac/linux You can open a terminal, under Windows you can open "run" and enter "cmd" to enter the directory where the CodeIgniter project is located.
Cd/path/to/project; index.php Tools Message
If you operate correctly, you should see Hello world!.
$ PHP index.php Tools Message "John Smith"
Here we pass a parameter to it, which is the same as using the URL parameter. "John Smith" is passed in as a parameter and displays:
Hello John smith!
CI framework-CLI execution PHP code