Autoloading custom Zend_Form_Element - php

I can't get Zend to autoload a custom form element class. I did things exactly as Marcin describes here (except that my classes start with 'Zend' and not 'my' but I'm getting this error:
Warning: include_once(Zend\Form\Element\Div.php) [function.include-once]: failed to open stream: No such file or directory
I have Zend_Form_Element_Div inside forms\elements\ and Zend_View_Helper_FormDiv inside views\helpers\
Basically, every folder in the error message is missng an 's', the right path is Zend\Forms\Elements\Div.php
I also have this in my bootstrap, though I'm not sure if it's necessary, but I'm also using this for my forms and models folder (and some others, but I don't think there's need to post them all):
<?php
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element',
'path' => 'elements'
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms'
)
));
?>
(Is there actually any other way of doing this autoloading? Instead of declaring every single folder?)
Update:
Element_Div in application/forms/elements/Div.php
In my forms init() method: $this->addElementPrefixPath('Element_', APPLICATION_PATH . '/forms/elements');
Error I'm getting: Fatal error: Class 'Element_Div' not found in C:\xampplite\htdocs\code\application\forms\PostForm.php on line 63

You essentially have to tell the form where to find custom elements by using:
$form->addElementPrefixPath()
In your case, you would use - either within the form's init() or __construct() method - something like:
$this->addElementPrefixPath('Zend_Form_Element_', APPLICATION_PATH . '/elements);;
However, I have agree with #Marcin. Naming your own classes with the Zend_ pseudo-namespace is ill-advised. Either:
Decide on an application namespace and declare it in your Bootstrap when you create your $resourceLoader
Create an custom library that resides on your include path - probably at the same level as the Zend library - and put your custom stuff out there.
Let me know if you need more details on either of these suggestions and I'll fatten up the explanations a bit.
Update based on comments
Using an empty appnamespace, your call to addElementPrefixPath() now changes to:
$this->addElementPrefixPath('Element_', APPLICATION_PATH . '/elements);
And I guess you could remove the elements entry from the $resourceLoader definition in your Bootstrap since it's really not doing anything.
Update 2
I assumed that you were adding the element to the form using the shortname, something like:
$form->addElement('div', 'my_div');
In this circumstance, we need to tell the $form and its plugin registry where to find an element of type 'div'. That's why we dealt with $form->addElementPrefixPath().
However, from the error message you are reporting, it appears that you are adding your custom element to the form using something like:
$div = new Element_Div();
$form->addElement($div, 'my_div');
In this case, it is not the $form and its plugin registry that has to worry about finding/loading/instantiating the custom element; it is the $autoloader via its $resourceLoader. In that case, there is no need for the $form->addElementPrefixPath(), which is essentially a hint to the form on how to find custom elements invoked by shortname.
What we need is to configure the $resourceLoader back in Bootstrap so it knows where to find the class. Assuming you stick with empty appnamespace (so your class is named Element_Div) and you place the file in application/forms/elements/Div.php, then the $resourceLoader call is as follows:
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model_',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element_',
'path' => 'forms/elements'
),
'form' => array(
'namespace' => 'Form_',
'path' => 'forms'
)
));
That should do it. [Famous last words, eh?]

I prefer creating forms like this:
$form->addElement(new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
or
$form->addElement($whatever = new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
$whatever->removeDecorator('Errors');
when I need to further modify the element.

Related

ZF2 Routes - The requested URL could not be matched by routing

I'm having trouble setting up a route for a very simple controller. I'm getting the "The requested URL could not be matched by routing." error. I've viewed similar solved questions on SO and can't pinpoint what I'm doing wrong (Ex: ZF2 - Zend Framework 2, understanding routing)
I've followed the skeleton tutorial with the albums subject and everything functioned perfectly fine. I tried duplicating the album module and then changing the name of the controller, folder, module config, etc. I figured this would be a good way to confirm I can at least replicate working code. I'm just trying to echo "123" to the page, so I tried eliminating the directories for forms, models and some of the views from the new module.
Is there some way to see what route I'm really looking for and what routes I defined? I know CI actually created a log file I was able to check. It was kind of like Apache logs but specific to framework functionality.
I'd like to post some of my code so someone could point out the mistake I am making and possibly explain why it is wrong. I tried paying close attention to case since different variations of the word album are used throughout the tutorial and I'm not 100% sure which ones are supposed to match up with what just yet. I'm trying to make it work for http://www.example.com/productbriefs.
Folder Structure
module.config.php:
return array(
'controllers' => array(
'invokables' => array(
'Productbriefs\Controller\Productbriefs' => 'Productbriefs\Controller\ProductbriefsController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'productbriefs' => array(
'type' => 'Literal',
'options' => array(
'route' => '/productbriefs',
'defaults' => array(
'controller' => 'Productbriefs\Controller\Productbriefs',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'productbriefs' => __DIR__ . '/../view',
),
),
);
ProductbriefsController.php
namespace Productbriefs\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class ProductbriefsController extends AbstractActionController
{
public function indexAction()
{
echo "123";
}
}
Module.php
namespace Productbriefs;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(),
);
}
}
As per my comment, you need to add Productbriefs to the module array in application.config.php or the module (including its configuration) will not be loaded.
To answer your second question, the controller manager needs to know how to load the controller classes your application uses. An 'invokable' is a class that can be instantiated without needing any arguments passed to it, so by adding controllers to that array you're telling the controller manager that it can instantiate that class simply by doing $controller = new Productbriefs\Controller\ProductbriefsController(). The key for the array is an alias, yes. This can be anything, although the ZF convention is to use the fully qualified name of the class but omit the 'Controller' suffix from the end. When you refer to controllers in your routing config you use these aliases.

Zend2 navigation

I would like to make navigation buttons in my view, for example index.phtml but it's not working. I did know how to do it in Zend1 but in Zend2 I have a problem. My code looks like this (file index.phtml):
$container = new \Zend\Navigation\Navigation($tableActions);
var_dump($container);
echo '<div class="table-column">';
echo $this->navigation($container)->menu();
echo '</div>';
Variable $tableAction looks like this:
public $tableActions = array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
);
I did not get any error, just whole site die on this line. var_dump returns object(Zend\Navigation\Navigation) so it's fine so far. Problem is, how to show it...
The navigation pages have dependencies which aren't being met by just creating a new container class in a view. The Mvc page needs a RouteStackInterface (Router) instance and a RouteMatch instance. Similarly Uri pages need the current Request instance.
You can see this clearly if you take a look at the Zend\Navigation\Service\AbstractNavigationFactory and its preparePages and injectComponents methods.
The view is not the right place to be instantiating menus, instead put the menu configuration spec in your module.config.php...
<?php
return array(
'navigation' => array(
'table_actions' => array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
),
),
);
Write a factory extending the AbstractNavigationFactory class and implement the getName() method which returns the name of your menu spec key (table_actions in this example)
<?php
namespace Application\Navigation\Service;
use Zend\Navigation\Service\AbstractNavigationFactory;
class TableActionsFactory extends AbstractNavigationFactory
{
/**
* #return string
*/
protected function getName()
{
return 'table_actions';
}
}
Map the factory to a service name in the service_manager spec of module.config.php ...
<?php
return array(
'navigation' => array(// as above ... )
'service_manager' => array(
'factories' => array(
'TableActionsMenu' => 'Application\Navigation\Service\TableActionsFactory',
),
),
);
Now you can call the view helper using the service name TableActionsMenu you just mapped
<div class="table-column">
<?php echo $this->navigation('TableActionsMenu')->menu(); ?>
</div>
Finally, if, as I suspect, you need to change an attribute of the page depending on the view, you can do that too, navigation containers have find* methods which can be accessed from the navigation helper and used to retrieve pages.
Here's an example looking for the page with a matching page label, then changing it before rendering (obviously not an ideal search param, but it gives you the idea)
$page = $this->navigation('TableActionsMenu')->findOneByLabel('On/Off');
$page->setLabel('Off/On');
// and then render ...
echo $this->navigation('TableActionsMenu')->menu();

Yii - how to access the base URL in main config

I am working on a Yii application. I am trying to set some paths in my main config params like this:
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
'paths' => array(
'imageTemp'=> Yii::getPathOfAlias('webroot').'/files/temp-',
'image'=> Yii::getPathOfAlias('webroot').'/files/',
...
),
'urls' => array(
'imageTemp'=> Yii::app()->getBaseUrl().'/files/temp-',
'image'=> Yii::app()->getBaseUrl().'/files/',
...
),
But I am getting this error:
Fatal error: Call to a member function getBaseUrl() on a non-object in ..blahblah../base/CApplication.php on line 553
I think I cannot use Yii::app() in config file since the app is not initialized yet here, or something like this. So, how can I replace Yii::app()->getBaseUrl() in the config file and get the same results?
You're right, you can't use the Yii::app() methods inside the config's return array, but you can use Yii::getPathOfAlias() outside. Something like this might work:
$webroot = Yii::getPathOfAlias('webroot');
return array(
...
'params'=>array(
'paths' => array(
'imageTemp'=> $webroot.'/files/temp-',
'image'=> $webroot.'/files/',
...
),
),
);
Assuming webroot is defined beforehand.
As for baseUrl... I'll come back to you on that one!
[Back...]
If you need a url, it all depends where your images files are being kept, relative to the yii path, or relative to the base of the web root?
If the base of the web root, you could just use:
return array(
...
'urls'=>array(
'paths' => array(
'imageTemp'=> '/files/temp-',
'image'=> '/files/',
...
),
),
);

ZendFramework 2 - how can i do the similar Bootstrap.php like in ZF1?

I have ZF 1 where i got working Bootstrap.php with lots of routing and other preDispatch stuff.
But in ZF2 there is no Bootstrap.php concept anymore? Or i mean how can i do this same in Zf2?
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initPdispatch() {
$this->bootstrap('frontController');
require_once APPLICATION_PATH . '/controllers/plugin/LanguageSelector.php';
$plugin = new LanguageSelector();
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin($plugin);
return $plugin;
}
protected function _initRoutes() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$dynamic1 = new Zend_Controller_Router_Route(
'/:variable1',
array(
'controller' => 'router',
),
// array('variable1' => '^[a-zA-Z0-9_-]*$')
array('variable1' => '^[\w.-]*$')
);
$router->addRoute('dynamic1', $dynamic1);
}
One of the best features of ZF2 is something that I actually hated at first, which are the routes. It's both great and annoying because now you're required to set the routes for all of your modules.
Part of understanding ZF2 (more quickly) is understanding modules. If you can get past this, you will begin to adapt much more quickly. (At least that's how it was for me). So, what in ZF2 is a module? Anything!
Anyway, all of the config files for every module and for the application eventually get merged within the Zend Framework, so that means you can define routes anywhere really.
That said, you don't need to "bootstrap" your routes anymore as that is part of your ModuleName/config/module.config.php file. now.
Now, I'm not an expert on regex routes within ZF2, but it would be something like:
// MyModule/config/module.config.php
return array(
'router' => array(
'routes' => array(
'dynamic1' => array(
'type' => 'regex',
'options' => array(
'route' => '/[:variable1]'
)
)
)
)
);
Somewhere in there you define the regex. Additionally, I saw in their docs that you can also define a regex route manually:
use Zend\Mvc\Router\Http\Regex;
// ...
$route = Regex::factory(array(
'regex' => '/blog/(?<id>[a-zA-Z0-9_-]+)(\.(?<format>(json|html|xml|rss)))?',
'defaults' => array(
'controller' => 'Application\Controller\BlogController',
'action' => 'view',
'format' => 'html',
),
'spec' => '/blog/%id%.%format%',
));
$router->addRoute($route);
You should be able to add this as a service or put it in onBootstrap() within the Application module if you're using the Skeleton Application.
Keep in mind, that was their example and again, I'm not an expert on this. Here is some more information.
Hope this helps!

Zend Framework 11.1: Form_Customer not found in

First of all, I know there are a few quite similar questions here on stackoverflow about that form problem but none of them could actually help me so I'm giving it a try myself. I've been stuck with this for the past 10 hours and I'm really desesperate right now.
So, let's start with my directory structure:
gpos/
---- application/
--- forms/
------- CustomerForm.php
--- modules/
--- default/
--- controllers/
--- CustomerController.php
--- views/
--- Bootstrap.php
---- public/
---- library/
--- Doctrine/
--- GPos/
--- Doctrine/
--- ActiveEntity.php
--- Models/
--- Customer.php
As you can see, I'm using Zend's standard forms/ folder to store my forms and I'm also using Doctrine as DB manager.
So I've been trying to reach my CustomerForm.php from CustomerController.php but simply getting a not found error.
Here is the error I'm getting:
Fatal error: Class 'Form_CustomerForm' not found in C:\wamp\www\GPos\gpos\application\modules\default\controllers\CustomerController.php on line 9
CustomerController.php:
class CustomerController extends Zend_Controller_Action {
public function init() {
/* Initialize action controller here */
}
public function indexAction() {
$form = new Form_CustomerForm(); //line 9
$form->setAction('/customer/index');
$form->setMethod('post');
$this->view->form = $form;
}
and CustomerForm.php:
class Form_CustomerForm extends Zend_Form {
public function init() {
and finally views/scripts/customer/submit.phtml:
<h2>Customer</h2>
<p>To create blabla</p>
<?php echo $this->form; ?>
I've been desesperately trying to add Form namespace to the autoloader but I learned today that it was already in thanks to Zend doing it for us. So... What am I doing wrong? I found tons of posts saying how to add resources and how to manage custom forms and I feel I'm doing exactly what's asked but it just won't work not matter what.
I'm afraid that it comes from somewhere else cause I've been trying to add different namespaces unsuccessfully too such as GPos_Doctrine for my ActiveEntity.php file in library/GPos/Doctrine/ with:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH,
));
$autoloader->addResourceType('gpos', '/../../library/GPos', 'GPos');
$autoloader->addResourceType('doctrine', '/../../library/GPos/Doctrine', 'GPos_Doctrine');
return $autoloader;
}
This didn't work either. I'm quite unsure of the 'path' parameter (2nd param) but I saw in a tutorial that the path of eath resource type you add must be relative to autoloader's basePath, so I came up with these paths.
The only way I could make my GPos_Doctrine_ActiveEntity() work was by adding autoloadernamespaces[] = "GPos_" to application.ini. Adding autoloadernamespaces[] = "Form_"didn't work though... I really don't understand what's wrong with my resource types I'm adding.
Please note that I didn't use zf tools to build that project. I'm considering doing it if I don't find a way to make it all work correctly.
I've also tried to rename my form class to "Application_Form_CustomerForm" but didn't do any good either. I feel I've tried everything I could now. I'm just deseperate :(
Oh and by the way, the only work around I found at first is to put forms/ folder in my library, that worked. Don't understand why, but I don't want to use that hack. See, I'm doing this project as my IT Bachelor work graduation and I really shouldn't get into hacking things to make them work :P.
Thank you for anybody paying attention!
either add an bootstrap to the default module (which in turn sets the resourcetypes automaticly)
class Default_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload() {
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default_',
'basePath' => APPLICATION_PATH . '/modules/default'));
return $moduleLoader;
}
}
or add this to your global bootstrap (extend it with your two custom resourcetypes):
public function _initDefaultResourceTypes() {
$al = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/modules/default/',
'resourceTypes' => array(
'dbtable' => array(
'namespace' => 'Model_DbTable',
'path' => 'models/DbTable',
),
'mappers' => array(
'namespace' => 'Model_Mapper',
'path' => 'models/mappers',
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms',
),
'model' => array(
'namespace' => 'Model',
'path' => 'models',
),
'plugin' => array(
'namespace' => 'Plugin',
'path' => 'plugins',
),
'service' => array(
'namespace' => 'Service',
'path' => 'services',
),
'viewhelper' => array(
'namespace' => 'View_Helper',
'path' => 'views/helpers',
),
'viewfilter' => array(
'namespace' => 'View_Filter',
'path' => 'views/filters',
)
)
));
$al->setDefaultResourceType('model');
}
take a look at this ZF 1 Skeleton for further info:
https://github.com/eddiejaoude/Zend-Framework--Doctrine-ORM--PHPUnit--Ant--Jenkins-CI--TDD-
I don't use modules but here what I do, which is very basic :
In /public/index.php, I add "/forms" to the include path :
(important part is the line with "forms" of course
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/forms'),
get_include_path(),
)));
My form are then named :
class Default_Form_Login extends Zend_Form
Then I would do :
$form = new Default_Form_Login();

Categories