CI framework-CLI execution PHP code

Source: Internet
Author: User
Tags codeigniter

What is the CLI:

The CLI (command line interface) is a text-based and computer-interactive approach. For more information, please see wikipedia .

Why use the CLI?

Although not obvious, there are a number of situations where we need to run CodeIgniter using the command line.

    • Use Cron to run tasks at timed intervals without using wget or curl
    • Use the return value of the function is_cli () to make your cron page inaccessible through the URL
    • Create interactive tasks such as: Set permissions, clear cache, backup, etc.
    • Integrate with other languages, such as the ability to invoke a directive in C + + to run code in your model.
Example:

Let's first create a simple controller, open your text editor, create a new file and name it tools.php, then enter the following code:

<? PHP class extends Ci_controller {    publicfunction message ($to = ' World ')    {         Echo "Hello {$to}!". Php_eol ;    }}

Then save the file to the application/controllers/directory.

Now you can access it by using a URL similar to the following:example. COM/index. php/Tools/message/ to

Alternatively, we can access it through the CLI. Under Mac/linux You can open a terminal, under Windows you can open "run" and enter "cmd" to enter the directory where the CodeIgniter project is located.

Cd/path/to/project;  index.php Tools Message 

If you operate correctly, you should see Hello world!.

$ PHP index.php Tools Message "John Smith"

Here we pass a parameter to it, which is the same as using the URL parameter. "John Smith" is passed in as a parameter and displays:

Hello John smith!

CI framework-CLI execution PHP code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.