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?' ?
Related
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.
I'm developing a app in laravel-4 PHP MVC framework
I'm wanting to develop some kind of utility class, for general coding tasks i carry out.
Such as: image uploading, image re-sizing etc... general application tasks
is it best practice to put all this in the base controller class? im thinking not, or defining a separate
UTILL::UtilityFunction();
// or
APP:UtilityFunction();
I'm not sure of the best way to structure this and keep it within best practice?
You're talking about a helper class, right? You better create classes to do whatever you need them to do, but they need to have a meaning on your app, they need to be specific, there's no problem creating a small class to do some image stuff and another one really small to upload files, but one utility class that does both is not good. Take a look at those articles: http://guru-php.com/blog/2008/08/128003/ and http://blogs.msdn.com/b/nickmalik/archive/2005/09/06/461404.aspx.
Using the same logic: you should add methods to your BaseControllers that are pertinent to all your controllers.
To create your utility classes, you can create a new directory (like app/library), create your classes inside it and add this path to app/start/global.php, in ClassLoader::addDirectories() list, Laravel 4 will autoload them automatically for you. Or you can add them to composer.json, using the autoload/classmap section and then run coposer dump-autoload to autoload them.
How to organize controllers under /app/controllers in sub-
folders in CakePHP? I want to create a folder like admin inside the controllers folder and I want to create some controller related to admin. If it is possible, then how can i call a controller from a sub folder?
You can use App::build() to let CakePHP know for additional packages/configurations.
App::build(array(
'Controller' => array('/path/to/controllers', '/next/path/to/controllers')
));
You need to re-think your application structure. Cake has something built in called prefix routing that you should probably be using.
This is also available in 1.x
You can't alter the CakePHP file structure "just like that". It would require serious modification of the core to achieve this, but there is almost never a good reason to do so. If you properly follow the naming conventions, everything should be easy to locate.
What you could do (that is still following conventions and comes close to what you're looking for) is create a plugin for all your admin related tasks and then you can put all that logic under app/Plugin/plugin_name/Controller instead. That way it has it's own place, although you will need to load the plugin from you main application for this to work.
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.