CakePHP 扩展控制器和重写方法
我正在开发一个应用程序,大部分代码将在未来的项目中返回。所以我的想法是创建一个这样的结构:
App/
- controllers
- models
- ...
Cake/
My_Custom_Folder/
- controllers
- models
- ..
基本上我想在普通 App 文件夹旁边添加另一个文件夹,并使用 App::build() 来设置额外文件夹控制器、模型等的路径。
现在的目标是仅当我的项目需要自定义工作时才使用应用程序层,我已经成功地做到了这一点,并且 CakePHP 会采用应用程序中的控制器而不是 My_Custom_Folder 中的控制器(如果存在)。
例如:如果我在 My_Custom_Folder 中有一个pages_controller.php,并且在我的 App 文件夹中也有一个,则它将使用 App 文件夹中的那个。如果 App 文件夹中没有,则使用 My_Custom_Folder 中的文件夹。
然而,这不是我希望它工作的方式,我想做的是从 My_Custom_Folder 扩展控制器,这样我就可以覆盖方法和/或添加新方法。
到目前为止,我已尝试以下操作:
/My_Custom_Folder/pages_controller.php
Class PagesController Extends AppController {
Public $name = 'Pages';
Public Function home(){
echo 'default home';
}
}
/App/pages_controller.php
require_once(ROOT . DS . 'My_Custom_Folder' . DS . 'controllers' . DS . 'pages_controller.php');
Class AppPagesController Extends PagesController {
Public Function home(){
echo 'Override default home';
}
}
不幸的是,这不起作用,它仍然加载默认主页。基本上我想要的是它从 My_Custom_Folder 加载所有方法,并允许 App 文件夹覆盖方法和/或添加方法,就像扩展 Appcontroller 一样。它确实加载了这两个文件,但它没有覆盖任何功能。
像这样的结构非常适合在许多项目中维护相同的代码,并且允许我通过更新 My_Custom_Folder 轻松地为所有项目添加新功能,并且它不会破坏具有一些自定义代码的项目。
对此的任何帮助将不胜感激。
I'm working on an Application, and most of the code will be returning in future project. So my idea was to create a structure such as this:
App/
- controllers
- models
- ...
Cake/
My_Custom_Folder/
- controllers
- models
- ..
Basically I want to add another folder next to the normal App folder and use App::build() to set the paths of the extra folder controllers, models, etc.
Now the goal is to only use the App layer when my project requires custom work, I have succeeded in that and CakePHP takes the controllers in the App over the ones in My_Custom_Folder if they are there.
For example: If i have a pages_controller.php in My_Custom_Folder AND one in my App folder, it will use the one in the App folder. If there is none in the App folder, then it uses the one in My_Custom_Folder.
However that is not how I want it to be working, what I want to do is extend the controllers from My_Custom_Folder so I can override methods and/or add new ones.
So far I have tried the following:
/My_Custom_Folder/pages_controller.php
Class PagesController Extends AppController {
Public $name = 'Pages';
Public Function home(){
echo 'default home';
}
}
/App/pages_controller.php
require_once(ROOT . DS . 'My_Custom_Folder' . DS . 'controllers' . DS . 'pages_controller.php');
Class AppPagesController Extends PagesController {
Public Function home(){
echo 'Override default home';
}
}
Unfortunately this doesn't work, it still loads the default home. Basically what i want is for it to load all methods from the My_Custom_Folder and allow the App folder to override methods and/or add methods the same way as you would by extending the Appcontroller. It does load both files, but it's not overriding any functions.
A structure like this would be great to maintain the same code over many projects and allow me to easily add new functionality for all projects by just updating the My_Custom_Folder and it would not break projects that have some customized code.
Any help on this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看插件、行为、组件或助手。通过插件,您可以将它们放在所有应用程序的一个通用文件夹中(就像您的 My_Custom_Folder 一样)。
Take a look at plugin or behaviors or components or helpers. With plugins you can put them in one common folder for all apps (just like your My_Custom_Folder).