溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Zend Framework之模型Model怎么用

發(fā)布時間:2021-09-02 09:36:13 來源:億速云 閱讀:85 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Zend Framework之模型Model怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Zend Framework之模型Model怎么用”這篇文章吧。

具體如下:

附一個簡單粗俗的例子。只是大概說明了用法:如果要深究,可以自己跟蹤源碼了解。

model_demo1

│  .project
│  .buildpath
│  .zfproject.xml

├─.settings
│      org.eclipse.php.core.prefs
│      .jsdtscope
│      org.eclipse.wst.jsdt.ui.superType.name
│      org.eclipse.wst.jsdt.ui.superType.container

├─application
│  │  Bootstrap.php
│  │
│  ├─configs
│  │      application.ini
│  │
│  ├─controllers
│  │      IndexController.php
│  │      ErrorController.php
│  │
│  ├─models
│  │      Test.php
│  │      ModelTest.php
│  │
│  └─views
│      ├─scripts
│      │  ├─index
│      │  │      index.phtml
│      │  │
│      │  └─error
│      │          error.phtml
│      │
│      └─helpers
├─docs
│      README.txt

├─library
│  ├─app
│  │      Test.php
│  │
│  ├─myApp
│  │      Test.php
│  │
│  ├─Zend
│  │      Test.php
│  │
│  ├─AppTest
│  │      Test.php
│  │
│  └─AppTest2
│          Test.php

├─public
│      index.php
│      .htaccess

└─tests
    │  phpunit.xml
    │  bootstrap.php
    │
    ├─application
    │  └─controllers
    │          IndexControllerTest.php
    │
    └─library

如下是從上到下,每一個文件的源碼,不再詳細(xì)說明:

/model_demo1/application/configs/application.ini

[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloadernamespaces.app = "App_"
autoloadernamespaces.my = "MyApp_"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

/model_demo1/application/controllers/IndexController.php

<?php
class IndexController extends Zend_Controller_Action {
  public function init() {
    /* Initialize action controller here */
  }
  public function indexAction() {
    var_dump ( Application_Model_Test::getUserInfo () );
    App_Test::echoAppTest ();
    MyApp_Test::echoAMyAppTest ();
    Zend_Test::echoZendTest ();
    AppTest_Test::echoAppTestTest ();
    $auto_loader = Zend_Loader_Autoloader::getInstance();
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath' => '/www/model_demo1/application',
        'namespace' => '',
        'resourceTypes' => array(
            'model' => array(
                'path' => 'models',
                'namespace' => 'Model'
            )
        )
    )
    );
    $auto_loader->pushAutoloader($resourceLoader);
    $auto_loader->registerNamespace(array('AppTest2_'));
    AppTest2_Test::echoAppTest2Test();
    Model_ModelTest::echoModelModelTest();
    exit ();
  }
}

/model_demo1/application/models/ModelTest.php

<?php
class Model_ModelTest{
  static function echoModelModelTest(){
    echo 'Model_ModelTest<br/>';
  }
}

/model_demo1/application/models/Test.php

<?php
class Application_Model_Test {
  static public function getUserInfo() {
    return array (
        'user_name' => '張三',
        'user_gender' => '男'
    );
  }
}

/model_demo1/application/Bootstrap.php

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
  protected function _initAutoload() {
    $app = $this->getApplication ();
    $namespaces = array (
        'AppTest'
    );
    $app->setAutoloaderNamespaces ( $namespaces );
    return $app;
  }
}

/model_demo1/library/app/Test.php

<?php
class App_Test {
  static public function echoAppTest() {
    echo 'App_Test<br/>';
  }
}

/model_demo1/library/AppTest/Test.php

<?php
class AppTest_Test{
  static public function echoAppTestTest(){
    echo 'AppTestTest<br/>';
  }
}

/model_demo1/library/AppTest2/Test.php

<?php
class AppTest2_Test{
  static public function echoAppTest2Test(){
    echo 'AppTest2Test<br/>';
  }
}

/model_demo1/library/myApp/Test.php

<?php
class MyApp_Test {
  static public function echoAMyAppTest() {
    echo 'MyApp_Test<br/>';
  }
}

/model_demo1/library/Zend/Test.php

<?php
class Zend_Test{
  static public function echoZendTest(){
    echo 'ZendTest<br/>';
  }
}

沒有貼出的代碼,是創(chuàng)建項目默認(rèn)的代碼。

以上是“Zend Framework之模型Model怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI