Heim  >  Artikel  >  Backend-Entwicklung  >  CodeIgniter CLI模式简介_php实例

CodeIgniter CLI模式简介_php实例

WBOY
WBOYOriginal
2016-06-07 17:18:52911Durchsuche

让我们以Hello World为例,首先创建一个简单的控制器。使用你的文本编辑器,创建一个叫做 tools.php 的文件,并且输入如下代码:

复制代码 代码如下:

class Tools extends CI_Controller {

  public function message($to = 'World')
  {
    echo "Hello {$to}!".PHP_EOL;
  }
}
?>

然后将这个文件保存到你的 application/controllers/ 文件夹里。现在正常情况下你可以通过你网站的 URL 来访问它:

复制代码 代码如下:

example.com/index.php/tools/message/to

除此之外,我们也可以在 Mac/Linux 中打开终端,或者在 Windows 下进入“运行”输入“cmd”,并进入我们的 CodeIgniter 项目的目录。

复制代码 代码如下:

$ cd /path/to/project;
$ php index.php tools message

如果你跟这样一步步运行下来,应该会看到 Hello World!。

复制代码 代码如下:

$ php index.php tools message "John Smith"

至此我们像使用 URL 参数一样给它传递了一个参数。“John Smith”作为一个参数被传递了,并且输出也变成:Hello John Smith!。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn