Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Phalcon加载助手文件库_Php_Phalcon - Fatal编程技术网

Php Phalcon加载助手文件库

Php Phalcon加载助手文件库,php,phalcon,Php,Phalcon,我已经创建了一个库,它将加载一个php文件(其中可能包含用户自定义函数…),您可以从引导调用它,也可以从控制器调用它。如果文件不存在,它将显示错误消息。问题是我是不是用现在的方式做的 如果我错过了什么,就给我指出来。。谢谢 Helpers是用户可以放置php文件的文件夹 app/ controllers/ models/ helpers/ library/ views/ 在“library/”文件夹中,有一个名为“helperfile.php”的php文件 //通

我已经创建了一个库,它将加载一个php文件(其中可能包含用户自定义函数…),您可以从引导调用它,也可以从控制器调用它。如果文件不存在,它将显示错误消息。问题是我是不是用现在的方式做的

如果我错过了什么,就给我指出来。。谢谢

Helpers是用户可以放置php文件的文件夹

app/
   controllers/
   models/
   helpers/
   library/
   views/
在“library/”文件夹中,有一个名为“helperfile.php”的php文件

//通过引导程序在每个页面上自动加载文件(“public/index.php”)

//从控制器加载它

$loadHelper = new helperfile();
$loadHelper->include_file([
    'calling_from_theController'
]);
public function productAction(){
    $productHelper = new productHelper();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->setDI($this->getDI());
    $helper->myFunction();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->myFunction();
}

这看起来很有效,但我认为你低估了法尔康能为你做的工作

帮助文件中的内容示例将非常有用。在本例中,我假设如下:

app/
   helpers/
           ProductHelper.php
在ProductHelper.php中

class ProductHelper{
    // code here
}
在有加载程序的引导程序中定义目录

$phalconLoader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$phalconLoader->registerDirs(
    array(
        $phalconConfig->application->controllersDir,
        $phalconConfig->application->modelsDir,
        // path to helper dir here
    )
)->register();
然后在你的控制器里

$loadHelper = new helperfile();
$loadHelper->include_file([
    'calling_from_theController'
]);
public function productAction(){
    $productHelper = new productHelper();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->setDI($this->getDI());
    $helper->myFunction();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->myFunction();
}
这应该行得通。它的代码更少,因此更简单,运行速度应该更快一些(使用phalcon的内置代码而不是编写一些php总是更快)

如果helpers中的代码不在类中,或者名称与文件名不同,那么它可能应该是。让事情变得简单多了

Di启用版本

class ProductHelper extends \Phalcon\DI\Injectable{
    public $config;

    public function myFunction(){
        $this->config = $this->getDI ()->get ('config');
    }
}
在控制器中

$loadHelper = new helperfile();
$loadHelper->include_file([
    'calling_from_theController'
]);
public function productAction(){
    $productHelper = new productHelper();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->setDI($this->getDI());
    $helper->myFunction();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->myFunction();
}
或者,在创建DI时

$di->set ('productHelper', function () use ($config, $di) {
    $helper = new ProductHelper();
    $helper->setDi ($di);

    return $helper;
});
在控制器中

$loadHelper = new helperfile();
$loadHelper->include_file([
    'calling_from_theController'
]);
public function productAction(){
    $productHelper = new productHelper();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->setDI($this->getDI());
    $helper->myFunction();
}
public function indexAction()
{
    $helper = new ProductHelper();
    $helper->myFunction();
}

谢谢CodeMonkey,这确实对我有帮助。非常有用的答案,谢谢,但我有一个问题,我如何在我的助手类中调用$di?