以CLI方式運(yùn)行
除了可以在瀏覽器中通過URL調(diào)用一個(gè) 控制器 外,也可以通過命令行接口(CLI)調(diào)用。
- 什么是CLI?
- 為什么使用這種方式?
- 它如何工作?
什么是CLI?
命令行接口是一種基于文本的和計(jì)算機(jī)交互的方式。 如果想查看更詳細(xì)說明,請(qǐng)看Wikipedia article.
為什么使用命令行?
雖然不是必須的,但在某些情況下我們會(huì)用到命令行
- Run your cron-jobs without needing to use wget or curl
- Make your cron-jobs inaccessible from being loaded in the URL by checking for $this->input->is_cli_request()
- Make interactive "tasks" that can do things like set permissions, prune cache folders, run backups, etc.
- Integrate with other applications in other languages. For example, a random C++ script could call one command and run code in your models!
Let's try it:? Hello World!
Let's create a simple controller so you can see it in action. Using your text editor, create a file called tools.php, and put the following code in it:
Then save the file to your application/controllers/ folder.
Now normally you would visit the your site using a URL similar to this:
example.com/index.php/tools/message/to
Instead, we are going to open Terminal in Mac/Linux or go to Run > "cmd" in Windows and navigate to our CodeIgniter project.
$ cd /path/to/project;
$ php index.php tools message
If you did it right, you should see Hello World!.
$ php index.php tools message "John Smith"
Here we are passing it a argument in the same way that URL parameters work. "John Smith" is passed as a argument and output is: Hello John Smith!.
That's it!
That, in a nutshell, is all there is to know about controllers on the command line. Remember that this is just a normal controller, so routing and _remap works fine.
?