I have created a library that which will load a php file (which may contains users custom functions in it...) you can either call it from bootstrap also from the controller. if file does not exists it will display the error msg. Thing is am i doing it in the currect way?
If i did miss any thing point me out.. Thanks
Helpers is the folder where users can put php files
app/
controllers/
models/
helpers/
library/
views/
In "library/" Folder a php file named "helperfile.php"
class helperfile extends Phalcon\Mvc\User\Component
{
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;
}
}
// to auto load file on every page through the bootstrap ("public/index.php")
$di->set('dispatcher', function () {
//Create/Get an EventManager
$eventsManager = new Phalcon\Events\Manager();
/*
* 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 Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
// To load it from controller
$loadHelper = new helperfile();
$loadHelper->include_file([
'calling_from_theController'
]);