2013-09-26 75 views
0

我試圖弄清楚是否有一種方法可以共享控制器,模型和多個CakePHP應用程序的視圖,但如果需要的話,可以覆蓋它們中的特定方法。因此,舉例來說,如果我有以下結構...CakePHP體系結構

/my_cool_app 
    /core 
    /app 
     /Model 
     Person.php 
     ->findPerson(); 
     ->mapPerson(); 
    /orgs 
    /org_1 
    /app 
     /Model 
     Person.php 
     ->mapPerson(); 

我想什麼做的是在org_1 /應用CakePHP的應用程序中使用的所有的控制器,模型,視圖等從/核心/應用程序,但是如果文件存在於該結構中(例如org_1/app/Model/Person.php),則提供覆蓋任何特定方法的功能。

這是可行的嗎?

回答

2

絕對有可能。你的目錄,例如:

/your_cool_app 
    /base 
     /app 
      /Model 
       AppModel.php 
       BasePerson.php 
    /inherited 
     /app 
      /Model 
       InherietdPerson.php 

然後繼承目錄的bootstrap.php中的內部可以使用App::build()告訴應用程序在何處尋找基本模型:

App::build(array(
    'Model' => array(
     //Path to the base models 
     $_SERVER['DOCUMENT_ROOT'] . DS 
      ."your_cool_app" . DS 
       . "base" . DS 
        . "app" . DS 
         . "model" 
    ) 
)); 

的BasePerson將延長AppModel:

<?php 

    App::uses('AppModel', 'Model'); 

    class BasePerson extends AppModel { 

    } 

?> 

而且InheritedPerson擴展BasePerson:

<?php 

    App::uses('AppModel', 'Model'); 

    class InheritedPerson extends BasePerson { 

    } 

?> 

現在來測試是否它的工作只是在繼承應用程序創建一個控制器和檢查,看看您的應用程序已加載哪些車型:

$this->set('models', App::objects('Model')); 

和視圖:

<?php 
    debug($models); 
?> 

它應該打印出如下內容:

array(
    (int) 0 => 'AppModel', //Is in the base app 
    (int) 1 => 'BasePerson', //Is in the base app 
    (int) 2 => 'InheritedPerson' //Is in the inherited app 
) 

檢出CakePhp的App class獲取更多信息。