Dynamic Default Module in Zend Framework - php

Does anyone know of a way to set the default module dynamically in Zend Framework and not run into namespace issues? For example, what I want to do is have a table of modules that are allowed to be loaded, with one of them set as the default module. For example, I may have:
admin
blog
calendar
as modules that can be loaded. If I have 'blog' as the default module, then 'admin' and 'calendar' have to have their controllers namespaced (Admin_IndexController, Calendar_IndexController) while 'blog' isn't (IndexController).
If I change 'calendar' to be the default module, ZF can no longer find the classes because of the namespacing.
How do you get around that? I'm currently using the following code:
$modules = new Modules();
$activeModules = $modules->fetchActive();
foreach($activeModules as $mod) {
$loadedModules[$mod->name] = '..application/modules/' . $mod->name . '/controllers';
if($mod->default) {
$defaultModule = $mod->name;
}
}
$frontController->setControllerDirectory($loadedModules);
$frontController->setDefaultModule($defaultModule);

If you plan on changing the default module, it is probably best to namespace ALL modules, and then specify that the default module should be prefixed:
First change the "blog" module to use namespacing:
<?php
// Used to be "class IndexController"
class Blog_IndexController extends Zend_Controller_Action {
}
Then, call setParam for prefixDefaultModule option on your instance of Zend_Controller_Front:
<?php
// Allow your default module to be prefixed
$frontController->setParam('prefixDefaultModule', true);
See bug # 1831 for an explanation.

use application.ini:
resources.frontController.prefixDefaultModule = true
resources.frontController.defaultModule = default

You can make a default module actually be the deciding part in this whole process.
More specifically - make all requests for default module go to a class that then will decide what specific module is currently default one and will re-route request to it.
At least that's the way we've implemented it ;)

Sounds like the work of a preDispatch Controller Plugin.
You can modify the request to change a module based on certain request or identity/session/known data to forward or redirect on demand.

Related

Zend Framework: Module Name in Bootstrap _initNavigation

I need to know how to get the current module name in the bootstrap _initNavigation() function of my zend application. On the load of the page I'm doing a request to a change navigation by module. The problem is that module name get in _initView() method by my code but module name not get in _initNavigation().
$this->bootstrap('frontController');
$req = $this->frontController->getRequest();
echo $req->getModuleName();
This can't be done in the bootstrap. Routing hasn't yet occurred, so the application doesn't know what the current module is. You'll need to move this logic to a controller plugin instead.
Try use getModuleName() method in bootstrap. That is return module name for current placed bootstrap (module or application)
This method placed in Zend_Application_Module_Bootstrap

Where do I save Zend_Form files?

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

How do I get Zend Framework to use 'module specific' configurations?

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"

Zend Router (How to direct url such as /administration/users to Administration_UsersController)

I would like to keep the old default Zend Router, and just add a router for administration subpages since the controllers are growing in size and I would like to logically separate them a little as well as have cleaner URLs.
The documentation seems to explain how to do other things but not this...
This will work out of the box with the default routes. You just need to add an administration module, and then /administration/users will map to the users controller in the administration module.
I don't know if its possibile to do it with an Underscore and the upcase user, sorry, but without you had to add the following to your bootstrap.php
$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
$route['admin_users'] = new Zend_Controller_Router_Route_Regex(
'administration/users',
array(
'controller' => 'administrationusers',
)
);
$router->addRoute('admin_users_route', $route['admin_users']);
note: in this scenario your controller is:
class AdministrationusersController extends Zend_Controller_Action
{
// stuff
}

How to use different bootstraping for different modules in zend framework

I have two modules, default and mojo.
After the initial bootstraping code which is the same for both of the modules, I want, for example, to use different layouts for each module (Or use different credentials check etc).
Where do I put this: IF(module=='mojo') do this ELSE do that
If you are using Zend_Application (in ZF1.8) then you should be able to use the module specific configuration options to provide this functionality with a as explained in the relevant section in the documentation.
This would require you to set the layout in the config so it looked something like
mojo.resources.layout.layout = "mojo"
anothermodule.resources.layout.layout = "anotherlayout"
The layout would then be set automatically by the bootstrap.
The other alternative is to use a front controller plug-in that implements the preDispatch() method to set the layout based on the module name.
hmm i havent tried this
http://www.nabble.com/Quick-Guide-How-to-use-different-Layouts-for-each-module-to23443422.html#a24002073
the way i did that now was thru a front controller plugin
something like
switch ($request->getModuleName()) {
case "":
// set layout ...
}
I've looked into the subject a couple of days ago, trying to get it to work on bootstrap config alone. The big problem is that all the bootstrap files are loaded, so it gives some weird results in which layout is used.
My conclusion was that you can have the config in place, but you need to work with FrontController plugins or ActionController helpers. If you want to use config set in the application.ini and you want to load the config trough the bootstrap, helpers is the only way to go. From the helper, you can then load the ActionController and on that execute the getInvokeArgs to load the bootstrap. A lot of hastle... :)
Anyway, I've done a small implementation as an example in a blog post: http://blog.keppens.biz/2009/06/create-modular-application-with-zend.html
Goodluck,
Jeroen

Categories