FuelPHP Updates

In working with FuelPHP recently a few thing have come up. The devs on #fuelphp on Freenode have been awesome at coming up with or accepting fixes. Here are the problems with immediate workarounds and long term solutions.

Fuel won’t let you refer to a View_Model with a templating engine extension

While View::forge('hello/world.twig') works fine, ViewModel::forge('hello/world.twig') fails to find the ViewModel because of the “.twig” part.

Workaround is to instead explicitly set the view on your ViewModel with public $_view = 'world.twig';

Long term fix is coming in Fuel 1.2.1 maintenance release https://github.com/fuel/core/commit/c06b0c67c027ee17bafa59cb089bd6c5baee80ab

Fuel insists that controllers are in the global namespace and prefixed with “Controller_”

No workaround.

Long term fix coming in Fuel 1.3 that will allow you to namespace and prefix controllers as you desire (as you already can with views) via a config setting: https://github.com/fuel/core/commit/882e564de66dc3f4edf83181b8fa36a8a8b083cd

Fuel’s View_Smarty does not expose the “plugins_dir” option.

Workaround is to set the option anyway, direct on the Parser object in your controller(s). The trouble is that accessing this object (and working out if it is Smarty-related) can be complicated. The naive version is something like:

$this->view->parser()->addPluginsDir(APPPATH.DS.'pluginsdirectoryname');

But if you want to continue to support non-Smarty rendering engines and are using ViewModels and Controller_Template it ends up looking more like (don’t judge me!):

        if (is_object($this->canvas)) {
            $rc = new \ReflectionClass($this->canvas);
            if ($rc->hasMethod('parser')) {
                $parser = $this->canvas->parser();
            } else if ($rc->hasProperty('_view')) {
                $parser = $this->canvas->_view->parser();
            } else {
                throw new YokoException('Unable to initialize Smarty i18n plugin.');
            }
            $smartyParam = 'plugins_dir';
            $ourPluginsDir = APPPATH . '/smarty';
            $pluginsDir = array_merge($parser->$smartyParam, [$ourPluginsDir]);
            $parser->$smartyParam = $pluginsDir;
        }

The long term fix is coming in the 1.3 release of Fuel’s parser package https://github.com/fuel/parser/pull/48. The devs were very good about accepting this patch which bodes well for future contributions to the project.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.