php – Phalcon加载助手文件库
发布时间:2020-05-31 01:18:13 所属栏目:PHP 来源:互联网
导读:我已经创建了一个库,它将加载一个php文件(可能包含用户自定义函数…),你也可以从控制器调用它来自bootstrap.如果文件不存在,它将显示错误消息.我是以现在的方式做的吗? 如果我确实错过任何指出我的事情..谢谢 Helpers是用户可以放置php文件的文件夹 app/ con
|
我已经创建了一个库,它将加载一个php文件(可能包含用户自定义函数…),你也可以从控制器调用它来自bootstrap.如果文件不存在,它将显示错误消息.我是以现在的方式做的吗? 如果我确实错过任何指出我的事情..谢谢 Helpers是用户可以放置php文件的文件夹 app/ controllers/ models/ helpers/ library/ views/ 在“library /”文件夹中一个名为“helperfile.php”的php文件 class helperfile extends PhalconMvcUserComponent
{
var $helper_Folder = '../app/helpers';
var $files = array();
public function __construct()
{
}
public function initialize()
{
}
public function include_file($files,$run = true)
{
if (!is_array($files))
$files = array($files);
foreach ($files as $file)
$this->files[$file] = $file;
if ($run)
$this->load();
}
public function beforeDispatch()
{
$this->load();
}
private function load()
{
if (empty($this->files))
return false;
foreach ($this->files as $file) {
$file = trim($file) . '.php';
if ($this->is_file_exists($file)) {
require $this->helper_Folder . '/' . $file;
}
}
}
private function is_file_exists($path)
{
$full_path = $this->helper_Folder . '/' . $path;
if (!file_exists($full_path)) {
$this->flash->error("Helper File Missing: " . $full_path);
return false;
}
return true;
}
}
//通过引导程序在每个页面上自动加载文件(“public / index.php”) $di->set('dispatcher',function () {
//Create/Get an EventManager
$eventsManager = new PhalconEventsManager();
/*
* Load Custom function files which are in the helpers folder
*/
$loadHelper = new helperfile();
$loadHelper->include_file([
'calling_from_bootstrap_1','calling_from_bootstrap_2'
],false);
$eventsManager->attach('dispatch',$loadHelper);
$dispatcher = new PhalconMvcDispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
//从控制器加载它 $loadHelper = new helperfile();
$loadHelper->include_file([
'calling_from_theController'
]);
这看起来很有效,但我认为你低估了Phalcon可以为你做的工作量.
辅助文件中的内容示例将非常有用.为了这个例子,我将假设它是这样的: app/
helpers/
ProductHelper.php
并在ProductHelper.php中 class ProductHelper{
// code here
}
在您拥有加载程序的引导程序中,您可以定义目录 $phalconLoader = new PhalconLoader();
/**
* 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();
然后在你的控制器中 public function productAction(){
$productHelper = new productHelper();
}
这应该工作.它代码较少,因此运行速度更快更简单(使用phalcon的内置代码而不是编写一些php总是会更快) 如果帮助程序中的代码不在类中,或者名称与文件名不同,那么它可能应该是.使事情变得简单得多. 启用版本 class ProductHelper extends PhalconDIInjectable{
public $config;
public function myFunction(){
$this->config = $this->getDI ()->get ('config');
}
}
并在控制器中 public function indexAction()
{
$helper = new ProductHelper();
$helper->setDI($this->getDI());
$helper->myFunction();
}
或者在创建DI时 $di->set ('productHelper',function () use ($config,$di) {
$helper = new ProductHelper();
$helper->setDi ($di);
return $helper;
});
并在控制器中 public function indexAction()
{
$helper = new ProductHelper();
$helper->myFunction();
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
