开发者

CakePHP Extending Controllers and Overriding Methods

开发者 https://www.devze.com 2023-04-06 16:03 出处:网络
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:

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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号