I started a project without using any framework.
My project structure was something like this:
A folder with classes to access my DB
A folder with classes for business logic
A folder with classes that can help me do other stuff
A folder with php pages that can be called inside pages(eg: navbar, footer, etc)
On root I had my php pages
So, calling all classes was preety easy. Now i am creating a new zf2 project and want to move all my work to this new project.I already setup the layouts and content of each page but i'm having some problems adding my custom classes. Should I use other kind of organization? Where can/should create my custom classes?
I also have some "php files" where I check some user info and depending on that, show him oriented advertisement. On my old project I was just calling it with "require"... can I do this on ZF2?/ is this the proper way to do it?**
Any help would be appreciated.
For anyone having the same question.
I created my custom library inside the vendor folder.
Here are some useful links:
http://code.tutsplus.com/tutorials/psr-duh--net-31061
http://ulaptech.blogspot.pt/2014/02/adding-3rd-party-libraries-to-zend.html
ZF2 follows the MVC (Model View Controller) design pattern.
So your directory structure looks like
module
Entity.php
EntityInterface.php
Table.php
TableInterface.php
TableFactory.php
The basic access methods are in Table.php
The mapping between columns and array is in Entity.php
If you would consult the tutorial Album application on the Zendframework website, you will see a working example.
Related
SHORT VERION: Where is the common place to store the controllers for dependencies in PHP?
LONG VERSION: I need to create a project at a high quality, coming from a framework background my knowledge on directory structure is a bit rusty and I would like some help.
Current Directory Structure: https://i.imgur.com/sxowces.png
I use the "controllers" driectory for controllers as callbacks for my routes. Each HTML page I create I assign a controller to its route. I ran into a problem when I had to create a "controller" for a dependencies, for example the Twig template engine. From past experience I've learnt that you shouldn't directly call to the twig library from a created instance, you should create a class that holds an instance and make calls through that class, am I correct?
This brings a new problem, I don't know how to structure this. I'm not sure if their called controllers or not, but where do I place these in my project directory structure? What is the common practice?
Putting them inside the controllers directory seems messy, as that's reserved for page controllers. Coming from a Laravel background, I've never had to do this. So my questions are, 'Is my current directory structure okay?', and 'Where do the "controllers" for dependencies go?' ?
I think my problem lies quite deep and I dont know if somebody can help, but...
This is my Application structure in my Zend project:
Application
Models
DbTable
configs
controllers
layouts
views
In Models directory i have f.e. Comment.php file which contains "Application_Model_Comment" class.
In DbTable dir i have f.e. Comment.php file which contains "Application_Model_DbTable_Comment" class.
And now, I can create an instance of that classes in controllers but can't get into these files in my IDE clicking ctrl key + mouse button. But I can use these classes in my app so it shouldn't be the classes autoloading problem.
As you can see, the example class contains a word "Model" when folder name is "Models", what personally suprises me. But classes are working fine.
So... do you have any idea why I can't use very good method to stepping into the classes and write phpDocumentation well? Other "classes links" like Zend_Something works fine.
PROBLEM SOLVED
I closed the project in Projects tab and opened it once again :)
I got a simple question and can't find the answer to it on the web.
Im using the framework CakePHP.
My question is:
Is it possible to split my model, controller and view directories.
Like this;
Controller
CMS
[controller files of the cms]
Front-end
[controller files of the front-end]
Model
CMS
[model files of the cms]
Front-end
[model files of the front-end]
View
CMS
[view files of the cms]
Front-end
[view files of the front-end]
I really hope somebody can help me, because i have searched in the document 2.x Cakephp and search it all over the web and i cant find a solution for it.
Thank you very much
Not as such, but you can have two different app directories (because you are essentially creating two different applications).
CMS
Controller
Model
View
...
Frontend
Controller
Model
View
...
You can set up both apps to use the same webroot directory.
Besides creating multiple app-s in the cake installation, you should consider Plugins as well:
app/Controller/...
app/Model/...
app/View/...
app/Plugins/CMS/Controller/...
app/Plugins/CMS/Model/...
app/Plugins/CMS/View/...
app/Plugins/Admin/Controller/...
app/Plugins/Admin/Model/...
app/Plugins/Admin/View/...
(...)
See the docs: http://book.cakephp.org/2.0/en/plugins.html
You could define with App::build() the additional directories in your app/Config/bootstrap.php file.
Alright, I know this is a bit localized, as I'm sure not many break out of traditional Zend Framework logic. But. This is a case where I have one main piece of software developed on ZF, and in it 3 different levels of platform.
So I have your stock folder structure of Zend Framework, then in it 2 additional sub folders that act as layers on top of the main structure. These sub folders have "layouts" "views" "controllers" and respectively "helpers" "scripts", etc.. so. With that due to how this was laid out I have run into a bit of a Jam, where I need to access a helper that is stored in one sub section from another sub section.
Normally you would access the helper like
$this->_helper->enrollHelper->isCreationDriven();
But, the controller I need to call this helper from is in another controller directory. Note I didn't build this app initially I am just helping enhance features and continue its growth. Anyway. The above line wont work for me in this case as the controller I want to call the helper from is outside of that directory in another like directory.
With that, My question is. Does anyone know a means I can call the helper in a similar fashion from this other directory? Or do I end up doing whats likely the obvious choice and just make a mirror copy of that helper in the other controllers directory where I want to call it from initially, my hope is there is a means as I want to avoid duplicate code.
During Bootstrap, you can register the second directory with the plugin broker using Zend_Controller_Action_HelperBroker::addPath($path, $prefix).
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.