Introduction to CodeIgniter CLI Mode

  • 2021-07-01 06:57:16
  • OfStack

Let's take Hello World as an example and start by creating a simple controller. Using your text editor, create a file called tools. php and enter the following code:


<?php
class Tools extends CI_Controller {

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

Then save this file to your application/controllers/folder. Now you can normally access it through URL of your website:


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

In addition, we can also open the terminal in Mac/Linux, or enter "Run" under Windows, enter "cmd", and enter the directory of our CodeIgniter project.


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

If you follow this step by step, you should see Hello World! .


$ php index.php tools message "John Smith"

So far, we have passed it a parameter like using URL parameter 1. "John Smith" is passed as a parameter and the output becomes: Hello John Smith! .


Related articles: