如何在cakePHP中编写可重用的代码?

RSK*_*RSK -1 php code-reuse cakephp

任何人都可以提供一些在PHP/CakePHP中编写可重用代码的技巧吗?

dec*_*eze 6

[如何]在AppController中编写单个[delete]函数

好的,我会咬人的.你的代码几乎完全一样,你会在各个控制器它的代码,你只需要更换的部件变化变数.

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

阅读Cake的源代码,了解哪些变量可供您使用.阅读更多源代码,特别是组件和行为,根据定义,可以重复使用,以了解更具体的技术.