【问题标题】:CakePHP ArchitectureCakePHP 架构
【发布时间】:2013-09-26 19:10:46
【问题描述】:

我试图弄清楚是否有一种方法可以为多个 CakePHP 应用程序共享控制器、模型和视图,但如果需要,可以覆盖其中的特定方法。所以,例如,如果我有以下结构......

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

我想做的是让 org_1/app 中的 CakePHP 应用程序使用 /core/app 中的所有控制器、模型、视图等,但如果文件存在于该结构(例如,org_1/app/Model/Person.php)。

这可行吗?

【问题讨论】:

    标签: php cakephp directory-structure


    【解决方案1】:

    绝对有可能。您的目录如下:

    /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 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2017-04-24
      相关资源
      最近更新 更多