通过 CLI 运行 Codeigniter 会输出网站根目录而不是预期结果

发布于 2024-12-08 10:11:18 字数 396 浏览 1 评论 0原文

我正在按照本教程通过 CLI 运行 Codeigniter。我已经完成了他们所做的一切(复制和粘贴),现在当我运行此命令时,它不会执行预期的操作,除了输出网站索引内容。

$ cd /Users/MyUsername/Sites/code
$ php index.php tools message

我得到的输出是索引页 HTML 源代码,例如 http://localhost/code

预期结果应该是

Hello World!

我怎样才能实现这个目标以使其发挥作用?

I'm following this tutorial on running Codeigniter via the CLI. I've done everything they've done (copied and pasted) now when I run this command, it doesn't do what is expected except it outputs the website index contents.

$ cd /Users/MyUsername/Sites/code
$ php index.php tools message

The output I get is the index page HTML source, e.g. http://localhost/code.

The expected result should be

Hello World!

How can I achieve this to make it work?

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

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

发布评论

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

评论(6

深海蓝天 2024-12-15 10:11:18

试试这个:

php index.php/controller/function/param1/param2/param3 etc

或者

php index.php controller function param1 param2 param3 etc

也可以发布 config.php 中 $_SERVER var 的内容和“uri_protocol”变量的值。

$config['uri_protocol'] 应该是 'AUTO'

try this:

php index.php/controller/function/param1/param2/param3 etc

or

php index.php controller function param1 param2 param3 etc

also, post the content of your $_SERVER var and value of the 'uri_protocol' variable from config.php.

$config['uri_protocol'] should be 'AUTO'

萌吟 2024-12-15 10:11:18

:来解决的

$config['uri_protocol'] = 'AUTO' 

对我来说,这是通过更改主配置文件中的

。我之前已将其更改为

'REQUEST_URI'

显然 不适用于 CLI

For me this was resolved by changing:

$config['uri_protocol'] = 'AUTO' 

in the main config file.

I had previously changed it to

'REQUEST_URI'

which doesn't work with CLI apparently.

著墨染雨君画夕 2024-12-15 10:11:18

我得到的输出是索引页 HTML 源代码,例如 http://localhost/code

您的路由出现错误,新的示例 CLI 控制器未触发,而是另一个控制器。很难说您的路由有什么错误,因为您没有显示完整的代码。

The output I get is the index page HTML source, e.g. http://localhost/code.

You have got an error with your routing, your new example CLI controller is not fired, but another controller. Which error you have with your routing is hard to say, as you have not shown your whole code.

吻泪 2024-12-15 10:11:18

如果您将 wget$this->input->ip_address(); 结合使用,您可以确保访问您的脚本的唯一计算机是您自己的网络服务器。它不如使用 php index.php 控制器函数在本地调用文件那么好,但它是一个后备方案。

以下是我创建的内容,它已经工作了几个月,没有出现任何问题:

  1. 在服务器上的某个位置创建一个目录,您可以将其用作 wget 创建的临时文件的暂存器。我在 public_html 文件夹下一层创建了一个名为 cron 的文件夹。

    例如。 /home/myuser/cron

  2. 构造您的 cron 命令。您可以使用 && 将命令串在一起。

    我。 cd /home/myuser/cron && -- 移动到您的暂存目录

    ii. wget http://www.example.com/cron/foo && -- wget 你的文件

    三. rm -f foo -- 从暂存目录中删除下载的文件“foo”

  3. 您的最终命令将如下所示:

    cd /home/myuser/cron && wget http://www.example.com/cron/foo && rm -f foo

  4. 检查访问您的 cron 文件的 IP 地址是否等于您的网络服务器的 IP:

     输入->ip_address() !== 'xxx.xxx.xxx.xxx'){
    
                 show_error('不允许直接访问');
    
             }
    
         }
    
         函数 foo($bar = 'bar')
         {
    
             echo $this->输入->ip_address();
    
         }
    
     }
    

重要提示:请确保您完全理解 rm -f 的效果。如果您不提供正确的文件,可能会产生有趣的后果。如果您有空闲时间,您可以选择不删除该文件,而只需定期手动删除所有 cron 临时文件。

If you use wget in conjunction with $this->input->ip_address(); you can ensure that the only machine accessing your script is your own webserver. It isn't as good as being able to call the file locally using php index.php controller function, but it is a fallback.

Here's what I have created which has worked for a few months without issue:

  1. Create a directory somewhere on your server that you can just use as a scratch pad for the temporary files created by wget. I created a folder named cron one level below my public_html folder.

    ex. /home/myuser/cron

  2. Construct your cron command. You can string together commands using &&.

    i. cd /home/myuser/cron && -- move to your scratch directory

    ii. wget http://www.example.com/cron/foo && -- wget your file

    iii. rm -f foo -- Remove the downloaded file "foo" from your scratch directory

  3. Your final command will look something like this:

    cd /home/myuser/cron && wget http://www.example.com/cron/foo && rm -f foo

  4. Check that the IP address accessing your cron files equals your webserver's IP:

     <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
     class Cron extends MY_Controller {
    
         public function __construct()
         {
    
             parent::__construct();
    
             // this controller can only be called from the IP xxx.xxx.xxx.xxx
             if ($this->input->ip_address() !== 'xxx.xxx.xxx.xxx'){
    
                 show_error('Direct access is not allowed');
    
             }
    
         }
    
         function foo($bar = 'bar')
         {
    
             echo $this->input->ip_address();
    
         }
    
     }
    

Important: Please make sure that you fully understand the effects of rm -f. It can have interesting consequences if you do not supply the correct file. If you have spare time, you could opt out of removing the file and just manually remove all of the cron scratch files periodically.

素衣风尘叹 2024-12-15 10:11:18

这对我有用 - 发现于 http://www.daharveyjr.com/codeigniter -cli-command-line-interface/

$config['uri_protocol'] = 'CLI';

然后我像这样运行

php index.php controller/function/parameter

This worked for me - found at http://www.daharveyjr.com/codeigniter-cli-command-line-interface/

$config['uri_protocol'] = 'CLI';

I then run like this

php index.php controller/function/parameter
蓝眸 2024-12-15 10:11:18

好吧,抱歉,但在此页面上 https://ellislab.com/codeigniter /user-guide/general/cli.html

他们没有提到我们放置codeigniter的文件夹的根路径还是controllers文件夹。我针对这个问题进行了很多搜索。我尝试了不同的事情然后我发现了这一点。

在此之前将 PHP 设置为 PATH 环境变量
{在 Windows CMD 中运行 php 文件}

PATH 环境变量后,

  1. 设置 cmd
  2. cd 你放置 codeigniter 的路径(即 CI/)
  3. php index.php/tools/message

你将

Hello World!

进入 cmd 屏幕。

Ok sorry to say but here on this page https://ellislab.com/codeigniter/user-guide/general/cli.html

They didn't mention whether the root path of the folder where we place the codeigniter or the controllers folder. I searched a lot over that issue. I tried different things and then I found that.

before that set PHP to your PATH environment variables
{Run php file in windows CMD}

after setting the PATH environment variables

  1. go to cmd
  2. cd the path where you put codeigniter (i.e CI/)
  3. php index.php/tools/message

you will get

Hello World!

on to your cmd screen.

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