开发者

How to write a reusable code in cakePHP?

Can any 开发者_如何学Goone provide me some tips to write reusable code in PHP / CakePHP?


[how to] write a single [delete] function in AppController

Okay, I'll bite. You'd code it almost exactly as you would code it in the individual controllers, you just need to replace the parts that vary with variables.

class AppController extends Controller {

    var $components = array('Session');

    function delete($id) {
        $model = $this->modelClass;  // holds the primary model name
        if (!$this->$model->delete($id)) {
            $this->Session->setFlash("Couldn't delete $model record $id.");
        }
        $this->redirect($this->referer());
    }
}

Read through Cake's source code to learn what variables are there for you to use. Read through more source code, especially of Components and Behaviors, which by definition are reusable, to learn more specific techniques.


Well, I don't think anyone mentioned components... this is really where the re-usable stuff should go in CakePHP, at least in my opinion, as I see it, the controller is unique, as is the model, and the component is where you put re-usable code to be called by the controllers.

It really depends on what you're trying to do, if you are just re-using code within a class (where it doesn't have any real utility outside of that class), then don't bother with a component, just use a function in the class.. however, when you find yourself using a function that is approximately the same in more than one controller, then put it into a class..

IMO the best thing is to just always try to break your classes into logical functions as you create them and then only worry about creating a component later on, out of a function in the class, meaning that if you aren't sure if you are going to re-use something, don't spend the extra time making it a component at first as that can easily be done later (but just takes a bit more work / thinking it through)...

if you know ahead of time you are going to be reusing a function then just go ahead and put it into a component to begin with.

(another thing in Cakephp that is great is that within the folders for model, controller, component... you can make your own folders and put files into them without cakephp caring where they are, as long as they are in the overall correct directory (i.e. controller, if its a controller) so, if you have a bunch of controllers that, say are for doing an action that you think of as "updating user profile information" then you just create a folder for this and put all those in there and you can do the same for components that relate to this (within the component directory)... but when you call a controller in this folder, you don't have to reference the folder in the calling url.. something I find very handy as it saves a lot of tedious work while allowing you to keep your class files very organized)


Create method in your app_controller.php and use it in other controllers.

// app/app_controller.php
public function _foo($params){
    // do something
}

// app/controllers/some_controller.php
public function foo($params){
    $this->_foo($params);
    // do something else
}

// app/controllers/other_controller.php
public function foo($params){
    $this->_foo($params);
}


Don't forget about the almighty plugin. I realize that this goes slightly against the comments in the main question. But the ultimate form of code reusability in cakephp is a plugin.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜