我使用cakephp版本3的应用程序。如何从自定义PHP文件调用函数。
假设我有一个自定义PHP文件 test.php 。我想从控制器文件 urlcontroller.php 。
test.php
<?php
error_reporting(E_ALL);
include 'path/to/UrlController.php';
echo json_decode(GetUrl());
urlcontroller.php
<?php
namespace AppController;
use AppControllerURLController;
use CakeEventEvent;
use CakeI18nTime;
use CakeNetworkExceptionNotFoundException;
use CakeORMTableRegistry;
class LinksController extends URLController
{
public function GetUrl()
{
$link = $this->Links->newEntity();
$data = [];
$link = $this->Links->patchEntity($link, $data);
if ($this->Links->save($link)) {
$content = [
'status' => '200',
'message' => 'success',
'url' => 'https://google.com'
];
$this->response->body(json_encode($content));
return $this->response;
}
}
}
尝试包含app index.php和bootstrap.php时,它仍然不起作用。
编辑test.php 基于@Salines答案,但仍然不起作用
<?php
namespace Apptest;
error_reporting(E_ALL);
use AppControllerURLController;
class Test extends URLController
{
public function custom()
{
$this->getUrl(); // call function from UrlController
}
}
错误:" php致命错误:class'app controller urlcontroller'在/../public_html/test/test/test.php中找到第7行7"
(我的测试文件位于/absolute/path/public_html/test/test.php上,我应该放在命名空间中?
与使用链接控制器
相同的方式test.php
<?php
namespace Apppath_to_folder_where_is_your_test_php;
use AppControllerURLController;
class Test extends UrlController
{
pubic function custom()
{
$this->getUrl(); // call function from UrlController
}
或没有类的PHP
$newUrl = new AppControllerURLController();
$result = $newUrl->getUrl();
但是,您不遵守MVC标准