I have a structure that is
application
application/modules
application/modules/default
application/modules/default/models
application/modules/admin
application/modules/admin/models
When calling controllers in admin I understand they must be named like Admin_TestController. This works fine, but my models in my admin module don't seem to be able to load. I have tried naming them and the files in all kinds of ways but it just doesn't seem to want to be able to load them. How should I name the file and model class in a module to be able to use it? I use autoloading.
What do your bootstrap files look like? That is the most import part of problem.
It is very important (in order for the namespaces to autoload) that you you have a bootstrap in each module (located # application/modules/admin/bootstrap.php) that should contain, at the very least:
class Reports_Bootstrap extends Zend_Application_Module_Bootstrap
Notice that it extends Zend_Application_Module_Bootstrap. This does the heavy lifting of registering the namespaces for the MVC of the module.
As Fatmuemoo states the bootstrap for the module should extend Zend_Application_Module_Bootstrap also you should include
resources.modules[] =
In your config. This is in the docs for Zend_Application_Resource_Modules
It seems you need to include a bootstrap class that extends Zend_Application_Module_Bootstrap for the modules you want to use. Check this forum post about a similar issue to see if it helps point you in the right way. Seems you may need more than one to load separate modules.
Related
I am developing a PrestaShop module, which will have it's own database tables. Let's say database table name is 'cat'. So I wanted to have a model class named Cat to keep track of it's properties and related operations. So where should it?
For example, there are prestashop core model classes inside classes directory. Is it ok to create a classes directory inside my module directory for that purpose? will it work?
The standard used is to place the model class in /module/model/YourModelClass.php, you can see this module and in your installation module class you should call it
require_once(_PS_MODULE_DIR_ . 'example/models/YourModelClass.php');
you have not a strict naming standard to your class model, like it does it the controller class and installation class.
Hope that it helps.
Cordially.
PrestaShop model structure is pretty free-flowing. You can decide what structure you want to use.
The only few constraints imposed on you are
having your module class which extends the PrestaShop module class;
registering the appropriate hook and their respective handlers;
My question was about where to place ObjectModel subclasses in prestashop. Above accepted answer is answering that question. But that's not enough to work the module correctly. You will have to include your model class where ever you want to use inside the module.
for example
include_once(_PS_MODULE_DIR_.'mymodule/classes/Cat.php');
class mymoduledisplayModuleFrontController extends ModuleFrontController {
// Other code goes here
}
If you are overriding existing model class, you can put your class inside /modules/your_module/override/classes directory. I have noticed while installing module, your overridden classes will be copied to the prestashop_root/override/classes directory.
http://doc.prestashop.com/display/PS16/Overriding+default+behaviors#Overridingdefaultbehaviors-Overridingaclass
I have some custom logic I need to insert into CDbCommand, CDbTransaction & CActiveRecord classes that comes with the Yii framework. I can't replace them because I am using the same framework files for other projects.
Most of my models are already generated and is extended from CActiveRecord. I know I can easily switch them to my own custom class extended from CActiveRecord. However, the methods that I want to override in CDbCommand would not be filtered into CActiveRecord if I extend the CDbCommand class, and in addition CDbCommand is used by many other Framework classes. I need all other framework classes to use my overrides of CDbCommand methods.
Any ideas?
There has been a similar question in the yii forums, concerning classes like CHtml, which are caled statically all over the framework and in generated Code.
There seems to be no pretty way, the kind-of-consensus in the discussion was to do the following (example CHtml):
Move CHtml.php to Html.php and rename the class to Html.
Create a new, empty CHtml class, that extends Html.
Add your overwrites to the new CHtml class.
It's not pretty but it works and is reasonably maintanable on Yii updates.
Override
To override CDbCommand and CDbTransaction, you should override CDbConnection. Most of my Yii Projects have many Yii classes overridden including classes that are mentioned in your question. It is a lot of work, but you have few choices.
For me, it is good practice to begin projects with overriding all classes you use. Once you write your project template with custom ActiveRecord, DbConnection, DbCommand and DbTransaction, Html and widget classes, there will be no need to solve problem of extending Yii classes again.
Fork
Of course, you always can fork and add custom logic directly to Yii classes or adjust something to allow extend classes easily. Sometimes, it is the simplest solution.
== Correct Answer Added below ==
ORIGINAL ANSWER
As far as Yii 1.1.* is concerned the best way seems to be to place the overriding versions of the files into a folder such as
/protected/component/overridden/ folder
and then to bind the application to the custom classes by adding them to import configuration in
/protected/config/main.php.
'import'=>array(
'application.components.CDbConnection',
'application.components.CDbTransaction',
'application.components.CDbConnection',
...
),
This would then call the custom class files even when called by the Framework files.
CORRECT ANSWER - RESOLVED
What I needed was the following at the end of index.php (in the root of project)
Yii::$classMap= [
'CActiveRecord' => dirname(__FILE__) .
'/protected/components/auditAndOps/CActiveRecord.php',
'CDbCommand' => dirname(__FILE__) .
'/protected/components/auditAndOps/CDbCommand.php',
'CDbTransaction' => dirname(__FILE__) .
'/protected/components/auditAndOps/CDbTransaction.php',
];
Yii::createWebApplication($config)->run();
What this essentially does is to remap the original classMap locations of the respective Framework files, to my custom files in /protected/components/auditAndOps/
Is there any way to create a function that works for all controllers in Codeigniter at init?
In Zend there is a application/Bootstrap.php, i need some solution like that.
You could extend the native CI_Controller class and create a MY_Controller class that all of your application's controllers would extend. Methods in the MY_Controller class would then be available to every controller that extends it. You could also put code in the MY_Controller constructor that would be executed each time a child controller was constructed.
I don't remember exactly how the Bootstrap file works in Zend, but if this sounds like a viable solution the Creating Core System Classes section of the documentation explains how to extend the native controller.
You can extend your New_controller to CI_Controller. In New_controller you can write common function which you want. For use about new extended controller you can see this link:
The subject of extending core controllers is discussed briefly in a few places in the manual - specifically in the Core Classes and Creating Libraries pages.
The intent of extending the core Controller is to provide methods and attributes to all your normal Controllers. There are other ways of providing this kind of pervasive functionality - including Hooks, Libraries and Helpers. You should familiarise yourself with the methods and benefits of those alternatives before assuming the following is the answer to your question.
Finally, it’s assumed that you have an application that does something - it doesn’t matter what, merely that you have an existing Controller that we can work with here.
-extend_the_CI_Controller
My site structure is currently like this:
/cms/
/data/ - caches etc
/modules/
/public/ - aliases from site public folders
/website1
/images
/layouts, views, scripts, css etc
(no index.php because of the alias)
This works fine, my system looks to the alias folder to run the app. However, I've now got to the point where I would like to extend one of the module controllers.
Ideally my structure would be:
/cms/
.. as above
/CLIENT_ID/
/modules
..extending/overriding scripts
However, this is the problem: the controller to be called must be called according to Zend's file name structure (Module_IndexController etc), so to extend the base classes would be:
Module_IndexController extends Module_IndexController
Which obviously wouldn't work nicely. My thought is that it should be:
CLIENTID_Module_IndexController extends Module_IndexController
But I'm stuck on ideas on how to implement this? I can add the controller directory using: addControllerDirectory on the front controller but I'm guessing I need to change the called class name somewhere?
I can then check if the folder is a directory and run the overriding class rather than the base one.
Any ideas? I'm open to restructuring the folders, but obviously need to keep media files in the public folder.
The problem is that you are not using the conventional directory structure that Zend_Dispatcher, Zend_Autoloader and Zend_Controller are programed to work with.
In my opinion you have two possible solutions:
customize Zend_Autoloader and Zend_Controller and Zend_Dispatcher. You can find possible customizations here, here and here.
modify the directory structure.
In this second case consider theese possible projectual choices:
create independent standalone zend framework projects for all the sites, and create in those the aliases of the CMS folders
like the previous but customizing the module locations with:
http://framework.zend.com/manual/en/zend.controller.modular.html#zend.controller.modular.directories
Use the CMS as a library: you put all the cms files in folder outside the /websiteN and then in websiteN's application.ini you set includePaths.library = /path/to/cms/folder". There are a lot of possible problems in this possibilty, that must be valuated knowing your project. In this way you can call models, extends websites's classes to extend cms's, but you cannot use your cms as the entry point and manager of the user interactions at variuos levels.
As you said, you can tell Zend the base controller directory for each module.
$controller = Zend_controller_Front::getInstance();
$controller->setControllerDirectory(
array("module_name" => "directory_path")
);
I would think Zend would want the naming convention to be ClientId_Module_Controller. But, if you really get stuck with the naming convention, and are using php 5.3 you could use namespaces.
I created an class extension of Zend_Controller_Action and added some user defined methods, which will be accessed from any controller so forth.
Every thing is working fine, until I use Zend Tool to create a new Action, as this time The Zend tool will not find out my extended class.
Error Message:
Fatal error: Class 'CMS_Zend_Controller_Action' not found in....
That is the class which extends Zend_Controller_Action and the one extended by other controllers like indexController.
How to make the class discoverable. Do I have to include each and every folders, like my classes are? Does zend does that? I dont think so. How does it do it?
Simple. :-p If it can find your core controllers, then you just need to include the path to your extended controllers.
http://php.net/manual/en/function.set-include-path.php
set_include_path(path_to_your_extended_classes) in your index.php, aka routes file.
I think what you are trying here is not what Zend_Tool is about.
As much as I understand your question and setup you have created a class in your library. Of course, you can extend Zend_Controller_Action with lots of your own classes in your own library/libraries (I do that, too). Adding an action to such a class is maybe unusual but a problem for Zend_Tool for one specific reason.
Zend_Tool I believe is only about the well known structures like /application and same for what is inside /modules. If you create a Controller Class Zend_Tool will do some work for you like adding required folder structure to your /application or /modules folder. Same with action method which require view files. Having a Controller Class in your library does not (should not) need all that and hence is not build into Zend_Tool. I think whatever class you create in your library is not supported in Zend_Tool.