I don't have a custom Zend_Form and I just declared all the elements in the ini file. There is no problem with creating the Zend_Form from the ini file, but I am having problem using my own custom validator in my ini file. It always return Not Found In Registry error.
Currently, my code is like this.
[Bootstrap]
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'MY',
'basePath' => dirname(__FILE__)
));
$resourceLoader->addResourceType('validator', 'forms/validate/', 'Form_Validate');
[ini file]
form.elements.new_password.options.validators.password.validator = "Password"
[Custom Validator]
<?php
class MY_Form_Validate_Password extends Zend_Validate_Abstract
{
......
Please tell me what I'm missing here. Thanks!
This is a case of over thinking:
I'm going to assume you're using a version of ZF1.x that's fairly new (v1.10 and newer).
You have a file called project/application/configs/application.ini
in a standard application.ini is the line:
includePaths.library = APPLICATION_PATH "/../library"
this tells us that all of our library code (a custom validator qualifies) would belong to the directory at this path.
Also there is the line:
autoloaderNamespaces[] = "MY_"
This tells us the name of our library (defines the directory below /library).
so our custom validator MY_Form_Validate_Password would live at:
project
/library
/MY
/Form
/Validate
Password.php
nothing else is required, all that bootstrap code is only needed if you are going to do something not anticipated by Zend Framework, also validators are not typically registered as resources.
The class can be called as
$validator = new MY_Form_Validate_Password();
as usual and any other access method should work as well.
Related
I'm trying to learn Zend Framework! I'm quite interested in it but I can't find a tutorial which says where it's suppoused to be a Zend_Form class stored! Maybe it's something quite straightforward but I can't get it yet...
I've seen tutorials about this:
<?php
class Form_Example extends Zend_Form
{
public function init()
{
// Great code here
}
}
But none of them said where this code goes????? In a file in which folder in the directory tree?? I've read and I understand and I've done a little example with modules, controllers, actions, layouts and I know the importance about name conventions and the folder structure. So where does this form class must go and how can I call it from a view??
Thanks a lot, I know this must be easy for someone who already knows how to work well with Zend Framework =)
The best way to do this is to let ZF do it for you. ZF ships with a command line interface for both windows and *nix.
At the command line you can type zf create form Example, ZF will then create an empty form named Example.php at it's default application level location.
Typically this will be at application/forms/Example.php and the classname will be Application_Form_Example.
If you need to have a form constructed in a module the command would be similar:
zf create form Example -m admin where -m indicates you want the file created in a module and admin is name of the module.
Forms are one of the predefined resources in Zend Framework and as such have a default location. There are several other resources that are predefined and have defaults.
The Module Resource Autoloader
Zend Framework ships with a concrete implementation of
Zend_Loader_Autoloader_Resource that contains resource type mappings
that cover the default recommended directory structure for Zend
Framework MVC applications. This loader,
Zend_Application_Module_Autoloader, comes with the following mappings:
forms/ => Form
models/ => Model
models/DbTable/ => Model_DbTable
models/mappers/ => Model_Mapper
plugins/ => Plugin
services/ => Service views/
helpers => View_Helper
filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class
"Blog_Form_Entry", it would look in the resource directory's "forms/"
subdirectory for a file named "Entry.php". 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.
I normally have all my forms in a forms folder, alongside the models, controllers, and views.
So, my file structure looks like:
application ->
configs
layouts
plugins
controllers
models
views
forms ->
form1.php
form2.php
Using them in your application isn't quite so simple. You must instantiate the form class in your controller, then pass the form to your view. So in your controller you want something like:
$form1 = new Application_Form_Form1($options);
$request = $this->getRequest();
if($request->isPost()) {
if($form1->isValid($post)) {
// form is valid, do form processing here
}
}
$this->view->form1 = $form1;
Then inside of your view file, you place the form:
<html>
<body>
<div id="body">
<?php echo $this->form1; ?>
</div>
</body>
</html>
At the heart of your question are the issues of:
autoloading
how the ZF autoloader works in general, and
how the ZF autoloader is configured by default in a standard ZF app
which are actually three distinct, though clearly-related, issues.
Assuming that you have the default ZF installation in which the appnamespace is set to "Application", then name your form class Application_Form_Example and store it in the file application/forms/Example.php.
Then you can instantiate (in a controller, for example) using:
$form = new Application_Form_Example().
Make sure that you have resources.modules[] = in application/configs/application.ini.
For additional discussion about autoloading, see https://stackoverflow.com/a/10933376/131824
I am trying to use a custom class I have created to send out mail so I can keep the controller files thin. I created my custom class and put it in the components folder. I then added:
'sendMail' => array(
'class'=>'application.components.SendMail.',
),
underneath main components in my main config file.
This should allow me to access the class directly correct? I try using:
Yii::app()->SendMail->MailerConfirmation();
and:
Yii:app()->MailerConfirmation();
and all I end up with is errors.
Can anyone tell me how I include a custom component? Maybe I am doing this all wrong?
First it should be:
'sendMail' => array(
'class'=>'application.components.SendMail',
),
Notice the absence of dot in the end "SendMail" instead of "SendMail.". Also, this configuration expects that you have php file SendMail.php, in protected/components directory that is a class with name "SendMail" and that this component extends CApplicationComponent. The component id will be with lower first letter, eg Yii::app()->sendMail and this will return instance of "SendMail" class. I do not know what MailerConfirmation is, but if this is a method of SendMail object, then you should access it like Yii::app()->sendMail->MailerConfirmation()
If this doesn't help, then please post some code and post the errors you are getting.
Note that if you are not going to set any component properties in the config file or use other CApplicationComponent features and your config file includes the default:
'import'=>array(
'application.models.*',
'application.components.*',
),
You can put your SendMail.php class in the components directory and it will autoload by calling it via:
$mailer = new SendMail();
then call your methods via:
$mailer->MailerConfirmation();
if you do want to use CApplicationComponent, you may want to look here or here for a couple examples, as well as the Yii tutorials.
I'm getting
Fatal error: Class 'Form_Login' not found in /route/to/project/application/controllers/AuthController.php on line XX
when instantiating the class From_Login inside the controller.
I suppose the form is not being autoloaded by the bootstrap class.
In my bootstrap file I have this method
protected function _initAutoload(){
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
return $modelLoader;
}
supposed to autoload my resources.
I'm using the default project structure.
-application
--controllers
---Authcontroller.php
--forms
---Login.php
when I created the form with zf tool it automatically set the name as Application_Form_Login then I erased the Application_ part since I'm using "" namespace. I doesn't work either way.
I've also tried setting appnamespace="" in the application.ini file but nothing happened
After trying over and over different options I got tired because it didn't work so I erased the project folder and started from the beginning whit zend tool and ... voilĂ , it works!
In my opinion it was a problem with zend tool and/or the .zfproject.xml file since I was adding some resources manually and some others with the zf tool.
use Zend modular structure and change your class name 'Form_Login' to 'Default_Form_Login' .
Currently plugins I write seem to work or not in random ways. Some do work, some don't, and some of them work part of the time, again with no obvious pattern. Even if all of the same are written and registered (apparently) in the same way. And I fail to find a proper documentation on where to put your plugins and how to register them in application.ini so I need to rely on examples I find in BLOGs or here. And chances are some of these examples assume things that may not be true in my code.
So I just give a simple example, and if you could suggest how to make it work, and give links to good articles on ZF plugins, I'd highly appreciate that...
Here's how I currently do things:
Directory structure:
/library
/Zend
/Plugins
Myplugin.php
/applications
/myApp
/configs
application.ini
/modules
/default
/controllers
/configs
/views, etc
/admin
/controllers
/configs
/views, etc
Bootstrap.php
/public_html
index.php
Myplugin.php contains one class:
Class Plugins_Myplugin extends Zend_Controller_Plugin_Abstract
{
public function init()
{
print 'If I can see this, it's finally working!';
}
}
application.ini relevant stuff looks like this:
includePaths.library = APPLICATION_PATH "/../../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
pluginpaths.plugins = "Plugins"
resources.myplugin =
resources.frontController.plugins.myplugin = Plugins_Myplugin
;//I assume Plugins_Myplugin should be resolved into library/Plugins/Myplugin.php with class Plugins_Myplugin inside, given present configuration. Most likely I am wrong somewhere
And the most common error usually is:
Fatal error: Class 'Plugins_Myplugin' not found in /whatever/nevermind/domains/mydomain/library/Zend/Application/Resource/Frontcontroller.php on line 117
Sometimes it is found if I "register" it with just resources.myplugin =, omitting whole *resources.frontController.plugins.myplugin = Plugins_Myplugin* line.
So apparently this is not the right way to register your plugins. What is wrong with this way, and what would be the good way (using application.ini)?
you should know that your custom namespace is "Plugins" and
consider adding these lines to your config :
includePaths.library = APPLICATION_PATH "/../library"
autoloaderNamespaces[] = "Plugins"
resources.frontController.plugins[] = "Plugins_Myplugin"
and make sure to delete these :
includePaths.library = APPLICATION_PATH "/../../library"
pluginpaths.plugins = "Plugins"
resources.myplugin =
resources.frontController.plugins.myplugin = Plugins_Myplugin
The method above wasn't working for me unless I changed:
autoloaderNamespaces[] = "Plugins"
to
autoloadernamespaces[] = "Plugins"
My directory structure is the Zend's default. My operating system is Windows 7 professional. I'm using WAMP...
Hope it helps as I can't (or don't know how to) comment on the answer above.
The best way for u it's create some like this:
-applications
+controllers
+layouts
+models
-plugins
**Myplugin.php**
+views
In application.ini:
includePaths.plugins = APPLICATION_PATH "/plugins"
how i call this plugin....
Easy way, only need this:
public function indexAction()
{
$Myplugin = new Myplugin();
}
Reading the section Zend_Application_Resource_Modules in the docs here:
http://framework.zend.com/manual/1.10/en/zend.application.available-resources.html
I noticed this:
You can specify module-specific configuration using the module name as a prefix or sub-section in your configuration file.
using this:
[production]
news.resources.db.adapter = "pdo_mysql"
news.resources.db.params.host = "localhost"
news.resources.db.params.username = "webuser"
news.resources.db.params.password = "XXXXXXX"
news.resources.db.params.dbname = "news"
To me this is a good idea. But, when I simply add these prefixes to certain things I want to be specific to my modules, nothing changes.
So my question is: How do I tell Zend Framework to actually use these module specific prefixes?
I use the following implementation of modules in Zend. It allows you to use "module-specific" configuration.
application/config/config.ini
-----------------------------
[production]
resources.modules[] =
By doing this, you're telling Zend_Application that you want to use the Modules Bootstrap Resource plugin. The Modules plugin will load a separate bootsrap class for each of your modules, excluding the default module. Therefore, you need to create a new bootstrap class for your second module.
application/modules/news/Bootstrap.php
-----------------------------
class News_Bootstrap extends Zend_Application_Module_Bootstrap {
//---------------------------------------
// Automatically load our resources
//
// NOTE: You don't have to add this, its
// just and example to show that you
// can customize the bootstrap
// process just for this module.
public function _initModuleResourceAutoloader(){
$this->getResourceLoader()->addResourceTypes(array(
'modelResource' => array(
'path' => 'models/resources',
'namespace' => 'Resource'
)
));
}
}
This "News_Bootstrap" class will now be loaded and executed during the bootstrap process.
The naming convention for this file is important as the Modules Resource plugin needs to be able to find the class. Note that you must name the file Bootstrap.php.
Finally, you'll notice that you're subclassing the Zend_Application_Module_Bootstrap rather than Zend_Application_Bootstrap_Bootstrap like you do in the main bootstrap.
Now, your module-specific configuration should work:
[production]
news.resources.db.adapter = "pdo_mysql"
news.resources.db.params.host = "localhost"
news.resources.db.params.username = "webuser"
news.resources.db.params.password = "XXXXXXX"
news.resources.db.params.dbname = "news"