Cakephp /Config/email.php into a Plugin - email

Hi i have a plugin called Contact and into it i have
/Config/email.php file.
Cake seems not to load that file.
In my main bootstrap.php file i tried this:
CakePlugin::loadAll(array('Contact'=>array('bootstrap'=>true, 'email'=>true, 'routes'=>true)));
the bootstrap.php and routes.php file are loaded, the email.php no
Thanks

That's not how CakePlugin::load/loadAll() works, there is no email option, only bootstrap, routes and ignoreMissing.
Check the coobook and the API documentation
http://book.cakephp.org/2.0/en/plugins.html#advanced-bootstrapping
http://api.cakephp.org/2.4/class-CakePlugin.html#_load
If you like to load more than one bootstrap file for a plugin. You can specify an array of files for the bootstrap configuration key...
So something like this should work for you:
CakePlugin::loadAll(array(
'Contact' => array(
'bootstrap' => array(
'bootstrap',
'email'
),
'routes'=>true
)
));
That would load the files /Plugin/Contact/Config/bootstrap.php and /Plugin/Contact/Config/email.php.
However it won't work in case that file contains an EmailConfig class definition and your app also loads the app/Config/email.php file where such a class definition already exists. In that case you should choose another way to define your email configuration settings.

Related

Where to add guard config information of zfc-rbac?

I am following this guide to add Zfc-Rbac in my application. But I can't figure out where to put this configuration information.
Application\config\application.config.files
or Application\modules\modulename\config\module.config.php
You can also put it in a global config file, ie application/config/autoload/zfcrbac.global.php
Either in application/module or application/vendor, place the config inside the ModuleName/config/module.config.php
Also make sure you have added the ZfcRbac to the application module list
'modules' => array(
'Application',
'ZfcRbac',
'DoctrineModule',
'DoctrineORMModule'

How to use Zend_Loader_Autoloader

I think this is a reasonably easy question however I just don't get autoloading in Zend framework.
Basically, I have the standard Zend project layout with application/models, application/controllers, application/views directories. I have also added an application/forms directory, and the classes that it contains will be named Application_Form_*
How do I register the new directory with the autoloader?
Thanks...
Kim
You don't need to register the new directory with the autoloader. If you create a form it should look something like like this:-
application/forms/Myform.php
class Application_Form_Myform extends Zend_Form
{
public function init()
{
//Put your code here
}
}
You can then instantiate your form like this (in your controller for example):-
$myform = new Application_Form_Myform();
Zend Framework will then autoload the class for you.
http://framework.zend.com/manual/en/learning.quickstart.create-form.html
There is an explanation of Autoloading in Zend Framework in the manual.
An extract from that:-
Zend Framework has borrowed an idea from ยป PEAR, whereby class names have a 1:1 relationship with the filesystem. Simply put, the underscore character ("_") is replaced by a directory separator in order to resolve the path to the file, and then the suffix ".php" is added. For example, the class "Foo_Bar_Baz" would correspond to "Foo/Bar/Baz.php" on the filesystem. The assumption is also that the classes may be resolved via PHP's include_path setting, which allows both include() and require() to find the filename via a relative path lookup on the include_path.
Which basically means that folders don't all need to be registered in the autoloader. It can quite happily find files in folders anywhere under the application or library/Zend folders so long as you follow the naming convention and proper casing.
The default folders under application/ that end with an 's' are special cases specifically dealt with in Zend_Application_Module_Autoloader::initDefaultResourceTypes() and should not be confused with the main autoloading mechanism.
Use $resourceLoader:
$resourceLoader->addResourceTypes(array(
'acl' => array(
'path' => 'acls/',
'namespace' => 'Acl',
),
'form' => array(
'path' => 'forms/',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model',
),
));
See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html

Zend Autoloading models issue

Zend framework.
I want to autoload my models classes inside models folder, from within bootstrap class.
These models doesnt actually use any namespace (so I have Ex. User.php file's class named User and so on..).
If I understood correctly I should use the Zend_Loader_Autoloader_Resource and I tried:
function _initLoaderResource()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,//points to the "application" path where resides "models" folder
'namespace' =>''
));
$resourceLoader->addResourceType('models', 'models/');
}
And I receive following 'Zend_Loader_Exception' message:
'Initial definition of a resource type must include a namespace'
My questions are:
Is this the right way to autoload models?
How should I manage resource code that doesn't follow Zend Framework coding standard?
Actually you probably don't want to use the resource autoloader for this, since (as you've discovered) it requires a namespace. The standard autoloader (which loads models from the include path) has an option setFallbackAutoloader which tells ZF that that autoloader should be used for any class not matching a namespace covered by another. So all you need to do is ensure your models directory is on the include path and set this option to true.
You are probably already using the standard autoloader for loading the Zend classes, so you'll probably want to modify your application.ini file to add your model directory to the include path, and then set the fallback option either in application.ini or in your Bootstrap class:
protected function _initAutoloader()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
return $autoloader;
}
Zend Autoloader uses namespaces to make sure you are not using the autoload process, on those classes you don't want. So you would have to choose a namespace for your classes.
You could start your classes with an application specific namespace, or a general one.
namespaces like 'My_' or 'App_' are general, yet for example if your application name is Job Board, you could use namespaces like 'JB_' in your class files.
You may also write your own autoloader (either a totally new one, or by extending the Zend autoloader) and register it as the SPL autoloader to bypass this.
Your class names does not have to follow the Zend Framework naming conventions, just make sure they have a namespace and register the namespace in the autoloader.
Here I attach a piece of my code that registers some resources to be autoloaded. I'm having multiple modules, and each module has a namespace regarding that module name. Please note that since there were many namespaces, I register them all in a loop.
$nameSpaceToPath = array(
'Application' => APPLICATION_PATH,
'Base' => APPLICATION_PATH . '/base',
'Store' => APPLICATION_PATH . '/modules/Store',
'Payment' => APPLICATION_PATH . '/modules/Payment',
'Admin' => APPLICATION_PATH . '/modules/Admin'
);
foreach($nameSpaceToPath as $ns => $path) {
$autoLoaderResource = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => $path,
'namespace' => $ns
)
);
$autoLoaderResource->addResourceType('controller','controllers','Controller');
$autoLoaderResource->addResourceType('model','models','Model');
$autoLoaderResource->addResourceType('mapper','models/mappers','Model_Mapper');
$autoLoaderResource->addResourceType('service','services','Service');
// I'm using _Util_ in the name of my utility classes, I place them in 'utils' directory
$autoLoaderResource->addResourceType('util','utils','Util');
$autoLoaderResource->addResourceType('plugin','plugins','Plugin');
$autoLoaderResource->addResourceType('form','forms','Form');
// I'm using _Exception_ in the name of my module specific exception classes, I place them in 'exceptions' directory
$autoLoaderResource->addResourceType('exception','exceptions','Exception');
$autoLoader->pushAutoloader($autoLoaderResource);
}
When you are defining a resource type by calling:
$autoLoaderResource->addResourceType('service','services','Service');
You are actually telling Zend Autoloader that you have a type 'service' (1st param), which is placed in the directory named 'services' (2nd param), and you are using 'Service' token in the class names to specify classes of this type.
The above code tells Zend Autoloader to search for class Store_Service_Core in the path 'APPLICATION_PATH/modules/store/services/Core.php'.
As you can see I have registered the general 'Application' namespace for the APPLICATION_PATH path. This means that each class, starting with Application_ would be autoloaded from the APPLICATION_PATH. So forexample I have a class named Application_Init which uses some initialization tasks, and now Zend autoloads it from the path APPLICATION_PATH/Init.php.

Zend Action helper

I am learning how to use Zend framework and realise that the action helper is something that would be useful.
I have set up a default installation of Zend on my machine, but I dont know where the helper file needs to go, what I need to put in the bootstrap file and how I use it. Can anyone point me in the right direction please - the ZF user guide is not to clear to me.
Thanks
John
Two thoughts for where to place your custom action-helpers:
In a separate, custom library
In the folder application/controllers/helpers
These ideas are not exclusive. Functionality that is general enough to work in multiple projects should probably be pulled into a separate library. But for functionality that is application-specific, there is an argument that it could be somewhere in the application folder.
#Jurian has already described the "separate-library" approach. For app-specific helpers, you can do as follows:
For a helper called myHelper, create a class Application_Controller_Helper_MyHelper in the file application/controllers/helpers/MyHelper.php. In Bootstrap, you have something like:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH,
));
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Application_Controller_Helper_');
return $autoloader;
}
Then your helper can be invoked in a controller by using:
$this->_helper->myHelper;
As you can see, this presumes you are using appNamespace 'Application'. If not, you can (must!) modify your class names to accommodate your circumstance.
Cheers!
You can place action helpers in your own library. Besides library/Zend where all the Zend stuff is around, you can create a library/MyLibrary folder (MyLibrary is arbitrary chosen) and put the action helpers there.
A good place is the library/MyLibrary/Controller/Action/Helper folder you need to create and place your action helper there (i.e. Navigation.php). In this file, create the class MyLibrary_Controller_Action_Helper_Navigation.
The next step is to add the action helper to the HelperBroker of the Zend Framework during bootstrap. Therefore, create a new method in your Bootstrap.php file and add this function:
protected function _initActionHelpers ()
{
Zend_Controller_Action_HelperBroker::addHelper(
new MyLibrary_Controller_Action_Helper_Navigation()
);
}
One last remark is you need to configure the use of this library by adding this rule to your application.ini:
autoLoaderNameSpaces[] = "MyLibrary_"
You can do this through your application.ini file like so
resources.view[] =
resources.view.helperPath.Default_View_Helper_ = APPLICATION_PATH "/views/helpers/"
Then in your views/helpers path you can create a file like Time.php. This file would contain the following code:
<?php
class Default_View_Helper_Time extends Zend_View_Helper_Abstract
{
public function time()
{
$date = new Zend_Date();
return $date->get(Zend_Date::TIME_MEDIUM);
}
}
?>
To use this in your view scripts you would use
<?=$this->time()?>
Which would display the current time using your new View_Helper
You can avoid having to register your action helper namespace and path within the Bootstrap.php by declaring them in the application.ini instead like so:
resources.frontController.actionHelperPaths.My_Controller_Action_Helper = APPLICATION_PATH "/controllers/helpers"
Simply replace My_Controller_Action_Helper with your desired namespace, and modify the helpers directory path accordingly.
The helper can be initialized the same way:
$this->_helper->myHelper;
As mentioned by the docs, registering the prefix or path of the helpers is usually preferred because helpers would not be initialized until they are called like in the snippet above.
Of course, instantiating and passing helpers to the broker is a bit
time and resource intensive, so two methods exists to automate things
slightly: addPrefix() and addPath().
http://framework.zend.com/manual/1.12/en/zend.loader.pluginloader.html
Adding the config entry to the application.ini follows the same suggested pattern.

having problems with Zend framework validators

I'm having problem with custom builded validator that does not returns any error. I copied a file NotEmpty.php form folder library/Zend/Validate, rename class Zend_Validate_NotEmpty to My_Validate_EmailConfirmation and put it into folder My/Validate.
If I call this class like
->setRequired(true)->addValidator('NotEmpty',true,array('messages' => array('isEmpty' => "bla")));
I get the correct error, but if i call it like
->setRequired(true)->addValidator('EmailConfirmation',true,array('mess ages' => array('isEmpty' => "bla")))->addPrefixPath('My_Validate','My/Validate/','validate');
i get nothing...
What am i doing wrong?
Thanks is advanced for your answers...
Have you tried to set in your bootstrap file your new namespace?
Zend_Loader_Autoloader::getInstance()->registerNamespace('My');
Also, why not you just extends the NotEmpty validator instead of duplicate the class?