I'm trying out the module-based architecture for my application, where the default module is what used to be the main application before.
modules/
default/
firstmodule
secondmodule
thirdmodule
Each module has its models, views, controllers. This is the default module as an example.
default/
models
views
controllers
IndexController
My problem is with the ErrorController that zend creates by default at application/controllers/ErrorController.php. I'd like to move it into the default module's controllers folder, but I wonder if it needs any special treatment, being the ErrorController and all?
Should I move it to modules/default/controllers/ErrorController.php,
or should I leave it at application/controllers/ErrorController.php
Edit: If I move it to default/controllers, then each of the other modules would not have an ErrorController. Does that mean that I'd have to define one for each module since the application itself doesn't have an ErrorController? and is it worth it to define one for each module?
How come you still have things in the old structure, rather than all the leftovers moved the default module.
My directory structure is like so:
/application/data/
/application/config/
/application/modules/balcms/controllers/
/application/modules/burn/controllers/
/application/modules/default/controllers/
/library/
/public/
...
Only special treatment is ensure your prefix is correct for your autoloader (this can be optional).
Related
I'm having an unusual autoloading problem with my Zend website. Up until now autoloading has been working a treat. Now though, I added a new file the project and autoloading just can't find it. I've reduced the problem to the minimal test case and was wondering if anyone could help me out.
In my website I have a the usual directory structure, like so:
site/
application/...
library/
Zend/...
PHPUnit/...
Ext/
Extras/
Test.php
Service/
Test.php
I've correctly set up auto loading (as per other helpful comments on StackOverflow) and registered the Ext_ namespace, which is proved by being able to correctly instantiate Ext_Extras_Test.
The problem comes when I try to instantiate Ext_Service_Test. Autoloading "failed to open stream". I've checked the correct spelling, listed the directory contents using find, ls, and the file explorer to make sure that the file exists in the correct place.
I just can't get it to &^%%£* find the file! Does anyone have any clues?
Found it you are trying to override a resource autoloader, even though it's not specified I'm pretty sure this will effect all namespaces:
42.3.2. 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
DbTable/ => Model_DbTable
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'm working on multi-website CMS in Zend Framework.
I've came to a point where I need to override module from application/ folder in my website/application folder.
A bit better explanation of my issue:
Here's tree of my application (important part):
library/
application/
module1/
controllers/
models/
....
module2/
controllers/
models/
....
websites/
website1.com/
application/
module1/
controllers/
models/
....
So what I need to do is that module1/ in websites/website1.com/application/ override module1/ in application, IF it exists. I want everything in module1/ in websites folder to override everything in main application folder.
I'd also like if there are 2 controllers in this module1 (for example IndexController and TestController) and if I put only TestController in websites folder under module1/controllers to override ONLY TestController from Application folder and to get IndexController from main folder.
Sorry if I failed to explain exactly what I'm trying to achieve. If there's something unclear, please ask.
Thank you.
Edit:
Okey, first of all - thanks for your comments.
Reason for having websites/ folder is, mostly because of vhost as I prefer that all of my websites have separate (public?) folders, and reason for having one library with application folder is because, obviously, upgrade reasons (so when I, for example, upgrade Zend - I don't need to upgrade it for every website).
I'll most likely rarely use overriding option for controllers, and yes, I'd even prefer if I could, for example, extend main Controller (for example - IndexController) and override some functions, but I thought that's way harder then override whole class.
Here's full structure of my application:
library/ - Library folder contains Zend and many other classes that I'll use in my application.
Zend - Zend Framework
MyCMS - Classes from my old CMS.
sites/ - Folder that contains websties.
website_1 - Website one.
application/ - Application folder for website one. If I need to redefine module or something. So, if I need to override module: main_module, I'll create folder main_module here with files that I want to override.
config/ - Configuration for website_1 - if I need to override, for example, application.ini
lang/ - Language files for this specific website.
templates/ - Templates folder for website (layouts and templates). By the way, I'm using smarty.
default/ - Main template.
layout/ - Layouts for Zend View.
css/
js/
images/
modules/
files/ - Place to upload files in, for this website. This will contain user avatars and stuff.
index.php - Main file that runs bootstrap and application.
Bootstrap.php - Inherited bootstrap. In case I need to override some functions from default bootstrap.
application/ - Main folder that contains application modules and stuff.
main_module/
configs/ - Module configuration.
config.ini
controllers/ - Controllers for this module.
modules/ - Submodules. There are like boxes that I display on website. For example, if my main module is "news", here, I'll make new sub-module to display box with statistics.
submodule/
services/ - XML/JSON/whatever service. If someone targets controller in services with specific parametars, it'll return response in requested format.
controllers/ - Services will only have controllers.
configs/ - Configuration for this submodule.
controllers/ - Controllers for this submodule.
models/ - Models for this submodule.
lang/ - Language files for this submodule.
template/ - Templates for this submodule.
helpers/
css/
js/
images/
index.html
models/ - Models for main module.
lang/
services/ - Main module will also have services. See submodule services for explanation.
controllers/
template/
helpers/
css/
js/
images/
index.html
Bootstrap.php - This is main bootstrap file that every website's bootstrap file will extend (and override some methods - if needed).
Update
Even though I highly discourage your directory structure, it's your application and you should structure it however you like.
To load multiple controllers, you should create a Front Controller Plugin to add paths to the controller directories stack
class My_Controller_Plugin_Paths extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
// Something like this...
// Would be best to load paths via config/database or somewhere
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
$dispatcher->addControllerDirectory('/path/to/website1.com/controllers')
->addControllerDirectory('/path/to/website2.com/controllers');
}
}
This is entirely untested, but you get the idea. Just make sure you register the plugin with the Front Controller in you bootstrap
I'd agree with #Laykes. This is a badly structured application.
I'm not sure of your exact requirements, but if it were my application, I would try to structure it like this:
/application
/modules
/default
/controllers
/IntexController.php // Default_IndexController
/Website1com
/IndexController.php // Default_Website1com_IndexController (possibly extends Default_IndexController)
Here you can see a properly structured class inheritance without creating totally separate, and probably duplicate, application folder.
Autoloading anything like this though totally depends on you and your priorities. You could do a number of things, each with their own +ve's and -ve's.
You could throw all paths into your include_paths in the order you want
Check files exist and load from a front controller plugin
To name a couple.
I have all my zend applications like this
/app/
/app/registration
/app/games
I need to create a URL for games like this
mydomain.com/games/game-1
How do I set up the controllers and views in this directory structure when its like a module or sub application?
/app/games
/app/games/configs
/app/games/controllers
/app/games/controllers/Game1Controller.php
/app/games/views
...
One way would be to use the existing module conventions:
application/
controllers/
views/
configs/
modules/
registration/
controllers/
views/
configs/
The good thing about this is that ZF is already set up to handle this to some extent by convention... If you do it another way you are going to have to modify things more.
In this layout the top level controllers, views, etc.. are the Default module, while all other modules are under the modules directory.
I would also make each game its own module. If you have common code used in all games make classes you can extend and put those in your library.
You can also use zf tool zf.sh create module yourModuleName to create the default directory structure for modules.
I am trying to auto-load classes from the "lib" directory inside of one specific module directory.
I have tried everything but to no avail...
One of the classes I would like to load is a class called visUser which inherits from myUser I have already made a factory.yml file in myapplication/modules/mymodule/conf but it doesn't load...
I am doing something wrong? or module level configuration files are not suported?
I am using symfony 1.2 with propel.
Factories are an application specific file and cannot be overloaded on the module level. It's a weird idea anyway.
Read the config handlers file to see what files are supported on the modules level configuration (symfony/lib/config/config/config_handlers.yml).
I am building a Symfony project, and have created a new plug-in named sfUtilsPlugin. I currently have a directory structure that looks like this:
sfUtilsPlugin/
modules/
sfSearchLucene/
actions/
config/
lib/
templates/
Now, in the sfUtilsPlugin/modules/sfSearchLucene/lib directory, I have an object called sfLucene. The idea was that this object is accessible from the Symfony auto loading mechanism, so that it can be instantiated from anywhere within the application.
However, simply adding the sfLucene.class.php file to the sfUtilsPlugin/modules/sfSearchLucene/lib directory does not appear to add it to the autoloader.
Does anyone out there know why this might be happening? Perhaps it is just not possible to automatically use objects stored in this location inside Symfony.
Any advice is appreciated.
Because you are adding this class in lib subdirectory of module sfLucene, it will be autoloaded only if current module is sfLucene.
You have two options:
put this class somewhere into sfUtilsPlugin/lib directory;
require them every time you need it