Zend Controller Action Helper not found - zend-framework

Been pulling my hair out on why this is not working. Trying to register a controller action helper following e.g. found in Zend docs, several posts here and sundry blogs. Attempts were made both in application.ini and Bootstrap.
The helper itself resides in APPLICATION_PATH . "/controllers/helpers". The file itself is called Scoping.php. In application.ini, appnamespace = "".
<?php
class Helper_Scoping extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
// code is here
}
}
First I tried in the application.ini:
resources.frontController.actionhelperpaths.Helper = APPLICATION_PATH "/controllers/helpers"
resources.frontController.plugins.Scoping = "Helper_Scoping"
Calling the following in my controller throws an exception with the message: "Action Helper by name Scoping not found":
$this->_helper->Scoping();
Then I tried the following in my Bootstrap (I tried both "Helper" and "Helper_" based on other examples I saw):
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Helper_'
);
Zend_Controller_Action_HelperBroker::addHelper(
new Helper_Scoping()
);
}
This time I get an uncaught exception, but same idea: "Fatal error: Class 'Helper_Scoping' not found in /Users/ppjd/Sites/dbos/application/Bootstrap.php on line 116"
Since there are so many working examples out there, I figure it must me missing something silly. Please SOS.

I don't try this but I think your helper class (inside your application structure: APPLICATION_PATH . "/controllers/helpers) should be 'Zend_Controller_Action_Helper_Scoping' instead 'Helper_Scoping'.

In the event that anyone happens upon this, here is what finally worked for me: it was a namespace issue. In the Bootstrap, I made this modification ahead of the addHelper:
$rl = $this->getResourceLoader();
$rl->addResourceTypes(array(
// ...other namespace settings...
'helper' => array(
'path' => 'controllers/helpers',
'namespace' => 'Helper',
),
));
Everything worked fine after that. Hope this helps someone else.
[Sometimes I feel like I spend more time trying to finesse the framework than actual application development.]

Related

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 Module Bootstrap does not load

I have a very strange case where my Module is working but my Module's boostrap is not being loaded.
Here is the segment in my application.ini for module autoloading:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
Here is the bootstrapper:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'User_',
'basePath' => APPLICATION_PATH .'/modules/user',
'resourceTypes' => array (
'model' => array(
'path' => 'models',
'namespace' => 'Model',
)
)
));
}
Structure of my modules
Application
--modules
----user
------config/
------controllers/
------models/
------views/
------Bootstrap.php
----admin
The problem here is that User_Bootstrap is not being loaded.
<?php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
Zend_Registry::set('debug', 'haha');
}
}
By doing a Zend_Registry::get('debug') on any controller, it doesn't recognize that the key was set in the module bootstrap. In fact any syntax error in the User_Bootstrap does not work.
I don't know why User_Bootstrap is not being autoloaded. This is driving me crazy because I've been researching for 5 hours and can't even get a blog post close to covering this case...
Speaking of which, my models and controller classes are being autoloaded fine.
Try the following...
Change your application.ini file to use
; lose the quotes
resources.modules[] =
See http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.modules
Remove the _initAutoload() method from your Application Bootstrap class. You don't need this as the module bootstrap will automatically create a resource loader for your User_ classes
Not sure but it might as simple as improper case.
--Modules is in your structure but you keep referring to it as /modules. These should match case.
I hope it's that simple.
Don't duplicate the function names of your main bootstrap in your module bootstrap, as far as I know in ZF 1.x all of the boostraps get processed every call and I think your _initAutoload in the main boostrap is overriding the module bootstrap.
try calling your function some different like _initModuleAutoload.
At least worth a shot :)
Have you tried disabling frontController directory in application.ini config file? Try commenting/deleting this line:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

zend_form config problem

I have a structure like follows:
/application
.....
--/modules
----/structure
------/controllers
--------/indexController.php
------/forms
--------/Department.php //here class Structure_Form_Department extends Zend_Form
in indexController.php
...
public function saveAction()
{
$request = $this->getRequest();
$form = new Structure_Form_Department();//<-- error
....
}
and i get error
Fatal error: Class 'Structure_Form_Department' not found
when try to zf enable form module - receive :
An Error Has Occurred
This project already has forms enabled.
i think this is a config-like problem... but do not understand what i need to do...
EDIT 1
found good solution here
but for some way zend starts repeat executing _init... functions from default bootstrap.php....
I was also facing a similar problem few months ago and I got the solution by writing following code :
In application.ini
autoloadernamespaces[] = "Structure_"
In Bootstrap.php
protected function _initAutoload()
{
$autoloader=new Zend_Application_Module_Autoloader(array(
'namespace' => 'Structure',
'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'Structure'
));
}
And at the index.php
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH),
get_include_path(),
)));
Please let me know if it doesn't works.....
I guess adding Application in-front of Structure_Form_Department will work.
ie
Application_Structure_Form_Department()
Or you may want to tell in the config.ini the from appnamespace = "Application" to appnamespace = ''.
I have some piece of code in github. You can see how the modules work.
$contactForm = new Contact_Form_Contact();
Name of form is
class contact_Form_Contact extends Zend_Form
All codes over github. Check it out .
https://github.com/harikt/blog/blob/master/application/modules/contact/controllers/IndexController.php
https://github.com/harikt/blog/blob/master/application/modules/contact/forms/Contact.php

why Zend Framework can't find my controller plugin

I'm trying to write controller plugin to check authentication.
I created class of plugin, put in Application directory, Application.php and registered in Bootstrap.php. But there is an error: Fatal error: Class 'Authentication' not found.
Where does Zend Framework look for plugins, how to tell it where it is?
//Application/Authentication.php
class Authentication extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
return;
}
self::setDispatched(false);
// handle unauthorized request...
}
}
//bootstrap
protected function _initAutoloader()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH,
'namespace' => ''));
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Common_');
return $moduleLoader;
}
protected function _initPlugins()
{
$controller = Zend_Controller_Front::getInstance();
$controller->registerPlugin(new Authentication());
$controller->dispatch();
}
Thank you.
I know the question is really old, but I´m leaving the answer in case someone else stumbles upon here like I did. Here is (from version 1 from 1.8 and up) how to register a plugin:
ZF follows the naming standard: A_B parses to A/B.php . For the plugin, ZF automatically looks into the "path to library", which means that it looks within the directory of your library (where your Zend library is). So the plugin should be as follows: library/Something/Whatever.php ... That´s one scenario. Then all you have to do in application.ini is add the following:
autoloaderNamespaces[] = "Something_"
resources.frontController.plugins.Whatever = "Something_Whatever"
Translated to your case would be:
autoloaderNamespaces[] = "Common_"
resources.frontController.plugins.Authentication = "Common_Authentication"
And your library structure should be:
library/Common/Authentication.php
hope this helps to anyone stumbling upon here!
--Regarding your post/question
The reason why it´s not "finding" the class it´s because it´s not loading with autoload. One reason might be that you're somehow violating the naming convention (Your Authentication file is not under directory Common_ , or the file name of Authentication class is not Common_Authentication ...). A quick fix would be to put:
//bootstrap
protected function _initAutoloader()
{
require_once 'Common/Authentication.php';
}
with this addes, _initPlugins() will be able to execute without problem. :)

Zend Framework modular app, can't load models for each module, autoloading models?

Is there a way to have models for each module? I have 3 modules, one is a "contacts" module.
I created a model for it in modules/contacts/models/Codes.php
Codes Controller
class Contacts_CodesController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->view->messages = $this->_helper->flashMessenger->getMessages();
}
public function indexAction()
{
$codesTable = new Contacts_Model_Codes();
}
Codes Model:
class Contacts_Model_Codes extends Zend_Db_Table
{
protected $_name = 'codes';
}
The error I get:
Fatal error: Class 'Contacts_Model_Codes' not found in /Applications/MAMP/htdocs/zf_site/application/modules/contacts/controllers/CodesController.php on line 26
thanks
I found the problem. I forgot to put a bootstrap file in with my contacts module.
Now it all works and I can have my modules use their own models.
class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
I've found the solution, I guess! :)
It's a problem when you add the next Resource in the application.ini file
resources.frontController.defaultModule = "Default"
and also you use some kind of parameters. I think that is a Bug.
The correct way to implement Modules is:
1 - Create your desired modules and the 'Default' Module with zf tool
2 - In apllication.ini tell ZF where the modules are and where the controllers of those modules are, with
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
Use the known
resources.modules = ""
And set:
resources.frontController.params.prefixDefaultModule = ""
It's important because zf tool set it to "1". Here is the bug. :)
And remember DO NOT PUT WHAT THE DEFAULT MODULE IS!!
3 - Create the bootstrap file for each module and put:
If my module is 'Evacol':
<?php
class Evacol_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
Save it to /modules/Evacol/ obviously
Take note of Evacol_... and ..._Module_Bootstr... THE NAME OF MY MODULE EXTENDING THE CORRECT CLASS.
Don't use the default value of bootstrap file created with zf tool. I did it :)
DON'T MODIFY ANYTHING ELSE. IT IS NOT NECESARY.
And voila! Trust me. It works!
It was Zend Framework 1.10.8
You have to register the 'Contacts_' namespace with the auto loader. You can use Zend_Application_Module_Autoloader for this.
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Contacts_',
'basePath' => dirname(__FILE__) . '/modules/cotacts',
));
This will create the following mappings for your module inside the basePath you provide.
api/ => Api
forms/ => Form
models/ => Model
DbTable/ => Model_DbTable
plugins/ => Plugin
If you are using Zend_Application to boostrap your application and it' modules you should not need this because the docs say that:
When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.
add
resources.modules[] =
To your config ini
I'm using version 1.9.
This is part of my bootstrap:
protected function _initAutoload() {
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/modules/default')
);
}
protected function _initAutoloaders()
{
$this->getApplication()->setAutoloaderNamespaces(array('Eric_'));
return $this;
}
protected function _initPlugins()
{
$this->bootstrap('autoloaders');
$this->bootstrap('frontController');
// register the plugin for setting layouts per module
$plugin = new Eric_Plugin_Modularlayout();
$this->frontController->registerPlugin($plugin);
return $modelLoader;
}
The plugin Eric_Plugin_Modularlayout sets the correct layout for each module.
I have 3 modules: default, ez, contacts.
The funny thing is, In a contacts action I can call the models in the ez/models dir. without a problem.