>  기사  >  php教程  >  Zend Framework 튜토리얼의 모델 사용에 대한 간단한 예

Zend Framework 튜토리얼의 모델 사용에 대한 간단한 예

高洛峰
高洛峰원래의
2017-01-03 13:42:171291검색

이 기사의 예에서는 Zend Framework 튜토리얼의 모델 사용법을 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 자세한 내용은 다음과 같습니다.

첨부된 내용은 간단하고 저속한 예입니다. 사용법을 간략하게 설명합니다. 더 깊이 알고 싶다면 소스 코드를 직접 따라가며 이해할 수 있습니다.

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 │ └─오류
│  │   error.phtml
│ │
│  └─helpers
├─docs
│  README.txt

├─라이브러리
│  ├─app
│ │ Test.php
│ │
│ ├─myApp
│ │ Test.php
│ │
│ ├─Zend
│ │ 테스트 .php
│ │
│ ├─AppTest
│ │ Test.php
│ │
│ └─AppTest2
│ Test.php

├─ public
│ .htaccess

└─tests
│ phpunit.xml
│ bootstrap.php

├─application
│ └─controllers
│ IndexControllerTest.php

└─library

다음은 추가 설명 없이 각 파일의 소스 코드를 위에서 아래로 나열한 것입니다.

/model_demo1/application/ configs/application.ini

/model_demo1/application/controllers/IndexController.php

/model_demo1/application/models/ModelTest.php

[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/models/ Test.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(
        &#39;basePath&#39; => &#39;/www/model_demo1/application&#39;,
        &#39;namespace&#39; => &#39;&#39;,
        &#39;resourceTypes&#39; => array(
            &#39;model&#39; => array(
                &#39;path&#39; => &#39;models&#39;,
                &#39;namespace&#39; => &#39;Model&#39;
            )
        )
    )
    );
    $auto_loader->pushAutoloader($resourceLoader);
    $auto_loader->registerNamespace(array(&#39;AppTest2_&#39;));
    AppTest2_Test::echoAppTest2Test();
    Model_ModelTest::echoModelModelTest();
    exit ();
  }
}

/model_demo1/application/Bootstrap.php

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

/model_demo1/library/app/Test.php

<?php
class Application_Model_Test {
  static public function getUserInfo() {
    return array (
        &#39;user_name&#39; => &#39;张三&#39;,
        &#39;user_gender&#39; => &#39;男&#39;
    );
  }
}

/model_demo1 /library/AppTest/Test.php

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

/model_demo1/library/AppTest2/Test.php

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

/model_demo1/library/ myApp/Test.php

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

/model_demo1/library/Zend/Test.php

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

게시되지 않은 코드는 프로젝트 생성을 위한 기본 코드입니다.

<?php
class MyApp_Test {
  static public function echoAMyAppTest() {
    echo &#39;MyApp_Test<br/>&#39;;
  }
}
기억하세요: 합의된 규칙을 따르면 불필요한 문제를 피할 수 있습니다.

이 글이 PHP 프로그래밍에 종사하는 모든 분들께 도움이 되기를 바랍니다.
<?php
class Zend_Test{
  static public function echoZendTest(){
    echo &#39;ZendTest<br/>&#39;;
  }
}

더 많은 Zend Framework 튜토리얼과 간단한 모델 사용 예를 보려면 PHP 중국어 웹사이트를 주목하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.