Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon loading helper file library

Tags:

php

phalcon

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'
]);
like image 400
Raj Avatar asked Mar 15 '14 10:03

Raj


1 Answers

That looks like it shoould work, but I think you underestimate the amount of work Phalcon can do for you.

An example of what is in the helper files would be useful. For the sake of this example I will assume that that it is like this:

app/
   helpers/
           ProductHelper.php

and in ProductHelper.php

class ProductHelper{
    // code here
}

In your bootstrap where you have the loader you define your directories

$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();

and then in your controller

public function productAction(){
    $productHelper = new productHelper();
}

That should work. It is less code, so is simpler should run a bit faster (using phalcon's built in code rather than writing some php will always be faster)

If the code in the helpers is not in classes, or not named the same as the filename, then it probably should be. Makes things a lot simpler.

Di Enabled Version

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

    public function myFunction(){
        $this->config = $this->getDI ()->get ('config');
    }
}

and in the controller

public function indexAction()
{
    $helper = new ProductHelper();
    $helper->setDI($this->getDI());
    $helper->myFunction();
}

Alternatively when creating your DI

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

    return $helper;
});

and in the controller

public function indexAction()
{
    $helper = new ProductHelper();
    $helper->myFunction();
}
like image 60
CodeMonkey Avatar answered Oct 20 '22 18:10

CodeMonkey