php ci 启动cli,以CLI方式运行 - [ CI用户指南2.1.0 ] - 在线原生手册 - php中文网

以CLI方式运行

除了可以在浏览器中通过URL调用一个 控制器 外,也可以通过命令行接口(CLI)调用。

什么是CLI?

为什么使用这种方式?

它如何工作?

什么是CLI?

命令行接口是一种基于文本的和计算机交互的方式。 如果想查看更详细说明,请看Wikipedia article.

为什么使用命令行?

虽然不是必须的,但在某些情况下我们会用到命令行

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:

class Tools extends CI_Controller {

public function message($to = 'World')

{

echo "Hello {$to}!".PHP_EOL;

}

}

?>

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.

翻译贡献者:

baiyuxiong, Hex, tinsn, yinsigan

最后修改: 2012-03-29 14:11:25

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值