Call Config::Load doesn't work in Fuelphp - fuelphp

I have a class in the path 'app/classes' called 'Helper.php'. And also I have a config file 'custom.php' in 'app/config'.
The problem is, when I call the config file, this return FALSE.
class Helper {
public static function actions_header ($ractive) {
return Config::load('custom');
}
}
The custom config file
return array(
'sidebar_entities' => array (
array(
'name' => 'Dashboard',
'icon' => 'icon-dashboard',
'url' => 'dashboard'
),
array(
'name' => 'Álbumes',
'icon' => 'icon-music',
'url' => 'albums'
)
)
);

You probably need something like this:
// load the config
Config::load('custom', true); // true - so you load the config to group 'custom'
// return array of items
return Config::get('custom');
I have not tested this, but something like this should work.

Tried to repeat the same code and everything works fine for me. FuelPHP 1.7.

Up until 2012-08-28, load() only returned the loaded data on the initial call. If you called load() and the file is already loaded, it returned false. Since then, it will not load the file again, but return what is already loaded.
So the question is: how old is the version of Fuel you are using? Given the date of the change, that would be < 1.3, which is very old...

I have run into this issue as well. I run the following code. (I am running Fuel 1.6)
Config::load('config_file_name') //returns config array
Config::load('config_file_name') //returns false
Then I run the following to load a sub array of the config file.
Config::get('config_file_name.attr') //returns nothing
Turns out I just didn't understand the fuel documentation. Thanks #huglester, your answer made it all make sense for some reason.
The documentation says:
// This merges the "custom" config file in with the root config.
Config::load('custom');
// This loads the "custom" config file in a group named "custom".
Config::load('custom', true);
So, when you run Config::load('config_file_name'), you can access the config sub arrays by using Config::get('attr'). This is because the 'config_file_name' is merged with the root config.
If you want to use Config::get('config_file_name.attr'), then you need to load the config file using Config::load('config_file_name', true)

Related

Set Module name, Controller name, and Action name in Zend framework?

I recently started programming with Zend Framework.I want to change module name, controller name and action name of a module in my framework through coding, while coding in the same framework.
Its name is Application(module & controller) and I want to change it to Institute. Is this possible through coding?
I searched through Google for help, but either i couldn't find it or understand it properly. Any help would be appreciated.
This is really just a case of renaming things:
Update all namespaces from Application to Institute in all the classes in the module including the Module.php
Update the name of the controller and it's entry in config/module.config.php
Make sure you update the name of your view directory if you have one in the module, ie view/application/index etc to view/institute/index and make sure you update the entry in module.config.php to the same path
Update name of Application directory to Institute
Update the name in the array of modules in modules.config.php or if you are using an earlier version application.config.php from under the modules key.
That's all I can think of you would need to do
******** EDIT ********
So the basic idea would be as follows:
Add a console in a new module (I've used zend mvc console but you should probably use https://github.com/zfcampus/zf-console as mvc console is deprecated)
module.config.php
<?php
namespace Rename;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'console' => array(
'router' => array(
'routes' => array(
'rename-module' => array(
'options' => array(
'route' => 'module rename <moduleNameOld> <moduleNameNew>',
'defaults' => array(
'controller' => Controller\IndexController::class,
'action' => 'rename'
)
)
)
)
)
),
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
];
IndexController.php
<?php
namespace Rename\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Console\Controller\AbstractConsoleController;
class IndexController extends AbstractConsoleController
{
public function renameAction()
{
$request = $this->getRequest();
// Make sure that we are running in a console and the user has not tricked our
// application into running this action from a public web server.
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
$moduleNameOld = $request->getParam('moduleNameOld');
$moduleNameNew = $request->getParam('moduleNameNew');
$module = file_get_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", 'r+');
$updatedModule = str_replace($moduleNameOld, $moduleNameNew, $module);
file_put_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", $updatedModule);
rename("module/$moduleNameOld", "module/$moduleNameNew");
}
}
This can be run like
php public/index.php module rename Application Institute
I've only done renaming the module here and renaming the namespace in the Module.php
to finish this off you would need to recursively find all .php files in the Application directory and loop over applying the string replace (which should be improved really). Then you could update the view and application level config too with similar logic and using the steps above.
The code I've written is pretty bad and probably insecure but might help you along the way

multiple instances of view helper plugin?

How do I instantiate multiple instances of a view helper plugin in Zend 2?
I want to return a new instance every time I call $this->pluginName(); from the view.
How do I return a new instance of the view plugin?
Add the service name to the getViewHelperConfig() shared configuration key in Module.php and set this value to false
Module.php
function getViewHelperConfig()
{
return array(
'shared' => array(
'MyViewHelper' => false,
),
'factories' => array(
'MyViewHelper' => 'App\View\Helper\MyViewHelperFactory',
)
);
}
By adding 'MyViewHelper' => false, the service manager (or View Helper plugin manager) will create a new instance of that service each time it is used.
The documentation states
shared An array of service name/boolean pairs, indicating whether or not a service should be shared. By default, the ServiceManager assumes all services are shared, but you may specify a boolean false value here to indicate a new instance should be returned.

Class Sofzo_From not found error when implementing Sozfo TinyMCE solution

I'm trying to implement TinyMCE to text areas using the solution mentioned in Sofzo. But when I try to extend the Sofzo_Form I get the following error :
Fatal error: Class 'Sozfo_Form' not found in /home/foldername/public_html/application/forms/PageForm.php on line 4
What I have done so far -
Uploaded the Sofzo files to library with following directory structure
/library
../Sozfo
../Form.php
../../Form
../../../Element
../../../../TinyMce.php
../../View
../../../Helper
../../../Exception.php
../../../../FormTinyMce.php
../../../../TinyMce.php
Loaded the classes in application.ini as
Autoloadnamaspaces[] = "Sofzo_"
And in bootstrap as
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Zend_');
$autoLoader->registerNamespace('SF_');
$autoLoader->registerNamespace('CMS_');
$autoLoader->registerNamespace('Sofzo_');
$loader = new Zend_Loader_PluginLoader();
$loader->addPrefixPath('Zend_View_Helper', 'Zend/View/Helper/')
->addPrefixPath('Storefront_View_Helper',
'application/modules/storefront/views/helpers')
->addPrefixPath('Sozfo_Form', 'Sozfo/');
$view=new Zend_View();
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
But when I try to extent the Sofzo_Form in Page_Form as
class Form_PageForm extends Sozfo_Form { }
This issue was solved thanks to Tim Fountain. But now when I load an element as
$this->addElement('tinyMce', 'message', array(
'label' => 'Message',
'required' => true,
'cols' => '50',
'rows' => '10',
'editorOptions' => new Zend_Config_Ini(APPLICATION_PATH . '/configs/tinymce.ini', 'moderator')
));
I get the following error
Plugin by name 'FormTinyMce' was not found in the registry
Read through several comments in original site and they are said to add
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
to bootstrap. I've already done that, but I'm guessing I'm not doing something right. Help is much appreciated.
I think the issue is ZF can't find the class because it doesn't know about the Sozfo_ namespace. You've attempted to register this namespace in two different ways, but both of them are incorrect.
In application.ini, you have:
Autoloadnamaspaces[] = "Sofzo_"
But this should be:
autoloaderNamespaces[] = "Sozfo_"
Then in the bootstrap you've tried to register it with:
$autoLoader->registerNamespace('Sofzo_');
but presumably this should be:
$autoLoader->registerNamespace('Sozfo_');
(note spelling). Which ever fix you apply you should only use one of these methods, as they do the same thing.
If it still doesn't work after that then there's an issue with your include_path.
Edit: To fix the view helper path, try this instead of the two lines you currently have:
$view = new Zend_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$stack = Zend_Controller_Action_HelperBroker::getStack();
$stack->push($viewRenderer);
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
This adds the helper path to a view object like you have but also supplies it to the view renderer (which is what renders all the view scripts). If you don't do this then the view renderer uses its own view object, so the view object you setup in the bootstrap is never used for anything.
If this doesn't work, try passing a full path as the first parameter to addHelperPath instead:
$view->addHelperPath(APPLICATION_PATH.'/../library/Sozfo/View/Helper', 'Sozfo_View_Helper');

Zend_Cache_Frontend_Page does not recognize output without echo

I am trying to fiddle around with Zend_Cache, so I added following code to my action (will be moved to bootstrap later, I guess):
$frontendOptions = array(
'lifetime' => 7200,
'debug_header' => true, // für das Debuggen
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
'cache_with_post_variables' => true,
)
);
$backendOptions = array(
'cache_dir' => '/tmp/'
);
$cache = Zend_Cache::factory('Page', 'File',
$frontendOptions, $backendOptions
);
echo "hej";
var_dump($cache->start('someid'));
Zend generates a cache file containing hejbool(false) now, but apart from that it does not cache my page. According to a German book about zend framework, false is correct when there is no cache available. true is only returned when a cache was found.
When I debugged within Zend_Cache_Frontend_Page.php directly, it went down to the bottom of the start()-method, meaning that nothing went wrong (id given) and no cache was found, so one had to be generated. This was done (I can see it in /tmp/), but without the needed content.
So why does not not cache the output from Zend_View, but only direct output via echo?
I do not call any explicit function to render the view, but this did not seem necessary anymore (my views are always rendered automatically according to controller and action). I tried it for both a standard XHTML template (index.phtml) and an RSS template (index.rss.phtml).
Any ideas? Do you need any other code fragments?
When using the Zend_Cache_Frontend_Page you have to enable the disableOutputBuffering option. The reason is that Zend_Cache_Frontend_Page uses ob_start with a callback and it has to be the first call to ob_start otherwise it leads to that strange behaviour you've encountered.
To enable it you can either set it in your Bootstrap with
Zend_Controller_Front::getInstance()->setParam('disableOutputBuffering', true);
or using the configuration file after your frontController-setup (here in the INI-style configuration):
resources.frontController.params.disableOutputBuffering = true

how do I set up a contextSwitch to generate a csv file

In my controller init method I have this
public function init()
{
//initialise the context switch action helper
$this->_helper->getHelper('contextSwitch')
->addContext('csv', array('suffix' => 'csv',
'headers' => array(
'Context-Type' => 'application/csv',
'Context-Disposition' => 'inline; filename="fooo.csv"',
'Pragma' => 'no-cache',
'Expires' => '0',
)))
->addActionContext('stockreport', 'csv')
->initContext();
}
In my stockreportAction I have disabled the layout and view render as follows.
public function stockreportAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
echo 'foobar';
}
I get a blank page when I call this file, I would like to generate this so in the example 'foobar' appears as a downloadable csv file. Could anyone advise
Zend_Magic is not implemented yet.
It is your responsibility to generate csv or any other output format.
in short: context switcher modify view script suffix, so view script for your context is stockreport.csv.phtml (if csv context is active, stockreport.phtml otherwise).
But in your example context switcher used in a wrong way. You don't switch context here.
You don't use viewscripts, you don't check if context active in your action.
Why your output is empty is a different question - probably at some point you clear content in Response object or there somewhere is a fatal error & error output disabled.
Update:
Also headers are Content-Type and Content-Disposition