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.
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 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.
What I have is the following db structure(tables):
lists[name,id]
list_items[title,list_id,content]
I've created the needed files and code(the MVC) needed to manage the first table(lists).
I also added the hasMany to the model class. At that point I am stuck.
What I need is a solution for managing each item (basic CRUD, I assume that complex management is just an advanced CRUD that I will find out how to do by myself).
I will be specific: since it's a content that have no place (but the admin) that it will be used by itself, should I -
create a full mvc structure for it? (can or should I implement it somehow[how?] in the lists package?
if not, how can I attach the tables? (since the use is about to be dropped in version 2)
would an element(cake concept/context) will be the appropriate way to create a view for such situation?
ANY insight will be appreciated.
If I undertant correctly, you want to create a CRUD part of this tables by yourself, without bake.
You need to write all the MVC estrucure and be carefull with the naming combention of cakephp http://cakebaker.42dh.com/2006/02/18/cakephp-conventions/
You need the model into app/models and also a a controller into app/controllers (remember naming combentions) and for each model you need a folder into /app/views.
Alfo, every, every function in your controller needs a view, even if this action doesn´t write anything to screen
I hope this was usefull.
Have you tried using Cake's bake feature? Your CRUD will be automatically created in about 2 seconds. I would also recommend you do the Blog tutorial to get a feel for scaffolding.
CakePHP is all about convention over configuration. Eg naming conventions for tables, controllers, models etc.. So much can be done automagically.