目录 搜索
欢迎 目录 快速参考图 基本信息 服务器要求 许可协议 变更记录 关于CodeIgniter 安装 下载 CodeIgniter 安装指导 从老版本升级 疑难解答 介绍 开始 CodeIgniter 是什么? CodeIgniter 速记表 支持特性 应用程序流程图 模型-视图-控制器 架构目标 教程 内容提要 加载静态内容 创建新闻条目 读取新闻条目 结束语 常规主题 CodeIgniter URL 控制器 保留字 视图 模型 辅助函数 使用 CodeIgniter 类库 创建你自己的类库 使用 CodeIgniter 适配器 创建适配器 创建核心系统类 钩子 - 扩展框架的核心 自动装载资源 公共函数 URI 路由 错误处理 缓存 调试应用程序 以CLI方式运行 管理应用程序 处理多环境 PHP替代语法 安全 开发规范 类库参考 基准测试类 日历类 购物车类 配置类 Email 类 加密类 文件上传类 表单验证详解 FTP 类 图像处理类 输入类 Javascript 类 语言类 装载类 迁移类 输出类 分页类 模板解析器类 安全类 Session 类 HTML 表格类 引用通告类 排版类 单元测试类 URI 类 User-Agent 类 表单验证 XML-RPC 和 XML-RPC 服务器 Zip 编码类 缓存适配器 适配器参考 适配器 数据库类 Active Record 类 数据库缓存类 自定义函数调用 数据库配置 连接你的数据库 数据库快速入门例子代码 字段数据 数据库维护类 查询辅助函数 数据库类 查询 生成查询记录集 表数据 事务 数据库工具类 JavaScript类 辅助函数参考 数组辅助函数 CAPTCHA 辅助函数 Cookie Helper 日期辅助函数 目录辅助函数 下载辅助函数 Email 辅助函数 文件辅助函数 表单辅助函数 HTML辅助函数 Inflector 辅助函数 语言辅助函数 数字辅助函数 路径辅助函数 安全辅助函数 表情辅助函数 字符串辅助函数 文本辅助函数 排版辅助函数 URL 辅助函数 XML 辅助函数
文字

CodeIgniter 用户指南 版本 2.1.0

编辑文档、查看近期更改请 登录 或 注册  找回密码
查看原文

以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:

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
上一篇: 下一篇: