CodeIgniter:如果控制器位于文件夹内,则从命令行运行脚本?

发布于 2025-01-06 04:25:41 字数 823 浏览 0 评论 0 原文

正如这里所述: http://codeigniter.com/user_guide/general/cli.html

页面如下: http://www.example.com/myController/myFunc/myParam

可以运行于命令行为:

php index.php myController myFunc myParam

我的 codeignitor 设置有一些文件夹来对控制器进行分组,可以这样说:

myFolder -> myPageController
         -> myAdminController

所以,url 变为:

http://www.example.com/myFolder/myController/myFunc/myParam

如何我在 CLI 上调用同样的东西? 像这样的东西:

php index.php "myFolder/myController" myFunc myParam

似乎不起作用。

As stated here :
http://codeigniter.com/user_guide/general/cli.html

A page like :
http://www.example.com/myController/myFunc/myParam

can be run on the command line as :

php index.php myController myFunc myParam

My codeignitor set up has some folders to group the controllers, lets say like this :

myFolder -> myPageController
         -> myAdminController

So, the url becomes :

http://www.example.com/myFolder/myController/myFunc/myParam

How do I call the same thing on CLI ?
Something like :

php index.php "myFolder/myController" myFunc myParam

Does not seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩耳倾听 2025-01-13 04:25:41

您可以尝试使用:

php index.php myFolder myController myFunc myParam1 myParam2 ...

You can try with :

php index.php myFolder myController myFunc myParam1 myParam2 ...
等风也等你 2025-01-13 04:25:41

请注意,类文件中的索引方法对于在 CLI 中正常工作是必需的,否则 codeigniter 将返回 404 错误。

;)

Note, the index method in class file is necessary for work properly in CLI, otherwise codeigniter will return 404 error.

;)

笔芯 2025-01-13 04:25:41

对于CodeIgniter 1.7(如果有人不幸不得不支持遗留项目),有一个提到的解决方案此处

从命令行运行 CodeIgniter

目标

正如标题所示,我们的目标是能够从命令行运行 CodeIgniter 应用程序。这对于构建 cron 作业或运行更密集的操作是必要的,这样您就不会受到 Web 脚本的资源限制,例如最大执行时间。

这就是我的本地 Windows 计算机上的样子:
屏幕截图来自原帖

上面的代码就像调用这个 URL:

http://www.example.com/hello/world/foo

黑客攻击

在 CodeIgniter 文件夹的根目录下创建一个“cli.php”文件:

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Command Line Only!');
}



set_time_limit(0);

$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];

require dirname(__FILE__) . '/index.php';

如果您在 Linux 环境中并希望使该脚本自行可执行,您可以将其添加为 cli.php 中的第一行:

#!/usr/bin/php

如果您希望特定控制器仅是命令行,则可以在控制器构造函数处阻止 Web 调用:

class Hello extends Controller {

    function __construct() {
        if (isset($_SERVER['REMOTE_ADDR'])) {
            die('Command Line Only!');
        }
        parent::Controller();
    }

    // ...

}

For CodeIgniter 1.7 (if someone is unlucky to have to support a legacy project), there is a solution mentioned here:

Running CodeIgniter from the Command Line

The Goal

Just like the title says, our goal is to be able to run CodeIgniter applications from the command line. This is necessary for building cron jobs, or running more intensive operations so you don't have the resource limitations of a web script, such as maximum execution time.

This is what it looks like on my local Windows machine:
Screenshor from the original post

The above code would be like calling this URL:

http://www.example.com/hello/world/foo

The Hack

Create a "cli.php" file at the root of your CodeIgniter folder:

if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Command Line Only!');
}



set_time_limit(0);

$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = $argv[1];

require dirname(__FILE__) . '/index.php';

If you are on a Linux environment and want to make this script self executable, you can add this as the first line in cli.php:

#!/usr/bin/php

If you want a specific controller to be command line only, you can block web calls at the controller constructor:

class Hello extends Controller {

    function __construct() {
        if (isset($_SERVER['REMOTE_ADDR'])) {
            die('Command Line Only!');
        }
        parent::Controller();
    }

    // ...

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文