简体   繁体   English

当Cake php项目位于根目录时,如何运行其他php文件?

[英]how to run other php file when cake php project is on root?

我在服务器的根目录上有一个蛋糕php项目,我需要放置一些应该单独工作的php文件。该怎么办?

If you want to access your files eg with browser, you have at least two options here: 如果要使用浏览器访问文件,则至少有两个选项:

  1. Put your file in CakePHP webroot folder, eg webroot/somefiles/test.php. 将文件放在CakePHP webroot文件夹中,例如webroot / somefiles / test.php。 You should be able to reach your script with yourdomain.com/somefiles/test.php 您应该可以通过yourdomain.com/somefiles/test.php访问脚本
  2. Assuming you are using Apache, configure virtualhost, and then reach your script using it. 假设您正在使用Apache,请配置virtualhost,然后使用它来访问脚本。 More info: Apache Docs - virtualhost 更多信息: Apache Docs-virtualhost

Standalone Scripts run from the CLI 从CLI运行独立脚本

If you want to know 'how' to do this in cake you should really consider using a shell if you are running from the command line. 如果您想知道如何在cake中执行此操作,那么如果您从命令行运行,则应该考虑使用shell。

Using Shells/Console in Cake 在Cake中使用Shell / Console

Shells solve the problems of one off scripts that need to be run from the command line. Shell解决了需要从命令行运行的一次性脚本的问题。

Files Requested via the Browser 通过浏览器请求的文件

If you are interested in reaching the file from the browser, I would advise you add the action to one of your controllers. 如果您有兴趣通过浏览器访问文件,建议您将操作添加到一个控制器中。 Even if you don't want to rewrite the script into cake, you should still run the request through a controller so eventually, you will be able to port the script into cake. 即使您不想将脚本重写为cake,仍然应该通过控制器运行请求,这样最终您就可以将脚本移植到cake中。 Or leverage any authentication or testing if needed. 或根据需要利用任何身份验证或测试。

If you have a standalone script that you want to load, make it into a library. 如果您有要加载的独立脚本,请将其放入库中。

App\\Lib\\MyUtility.php

<?php
namespace App\Lib;
class MyUtility
{
    public function doThings()
    {
        echo "Hey I am doing things";
    }
}

App\\Controller\\UtilitiesController.php

<?php
namespace App\Controller;
use App\Controller\AppController;
use App\Lib\MyUtility;
class UtilitiesController extends AppController
{
    public function doThings()
    {
        (new MyUtility())->doThings();
        // You can exit here or actually give some feedback to the browser if needed
        exit;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM