Hello World

zfコマンドでプロジェクトの雛形を作成する

  1. C:\Tools\Works\php>zf create project sample01
  2. Creating project at C:/Tools/Works/php/sample01
  3. Note: This command created a web project, for more information setting up your VHOST, please see docs/README
  4.  
  5. C:\Tools\Works\php>
  6.  
  7.  

ディレクトリ一覧確認


sample01
   +.zfproject.xml
   +application
       +Bootstrap.php
       +configs
           +configs\application.ini
       +controllers
           +ErrorController.php
           +IndexController.php
       +models
       +views
           +helpers
           +scripts
               +error
                   +error\error.phtml
               +index
                   +index\index.phtml
   +docs
       +README.txt
   +library
   +public
       +.htaccess
       +index.php
   +tests
       +application
           +controllers
               +IndexControllerTest.php
   +tests
       +bootstrap.php
       +library
       +phpunit.xml


フロントコントローラー

  1. <?php
  2.  
  3. // Define path to application directory
  4. defined('APPLICATION_PATH')
  5. || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
  6.  
  7. // Define application environment
  8. defined('APPLICATION_ENV')
  9. || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
  10.  
  11. // Ensure library/ is on include_path
  12. set_include_path(implode(PATH_SEPARATOR, array(
  13. realpath(APPLICATION_PATH . '/../library'),
  14. )));
  15.  
  16. /** Zend_Application */
  17. require_once 'Zend/Application.php';
  18.  
  19. // Create application, bootstrap, and run
  20. $application = new Zend_Application(
  21. APPLICATION_ENV,
  22. APPLICATION_PATH . '/configs/application.ini'
  23. );
  24. $application->bootstrap()
  25. ->run();
  26.  
  27.  

.htaccessファイル

  1. RewriteEngine On
  2. RewriteCond %{REQUEST_FILENAME} -s [OR]
  3. RewriteCond %{REQUEST_FILENAME} -l [OR]
  4. RewriteCond %{REQUEST_FILENAME} -d
  5. RewriteRule ^.*$ - [NC,L]
  6. RewriteRule ^.*$ index.php [NC,L]
  7.  
  8.  

アクションコントローラー

IndexController.php

  1. <?php
  2.  
  3. class IndexController extends Zend_Controller_Action
  4. {
  5.  
  6. public function init()
  7. {
  8. /* Initialize action controller here */
  9. }
  10.  
  11. public function indexAction()
  12. {
  13. }
  14.  
  15. public function helloAction()
  16. {
  17. // リクエストパラメータを取得
  18. $request = $this->getRequest();
  19. $this->view->assign('name', $request->getPost('name'));
  20. }
  21. }
  22.  

ErrorController.php

  1. <?php
  2.  
  3. class ErrorController extends Zend_Controller_Action
  4. {
  5.  
  6. public function errorAction()
  7. {
  8. $errors = $this->_getParam('error_handler');
  9.  
  10. if (!$errors || !$errors instanceof ArrayObject) {
  11. $this->view->message = 'You have reached the error page';
  12. return;
  13. }
  14.  
  15. switch ($errors->type) {
  16. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
  17. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
  18. case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
  19. // 404 error -- controller or action not found
  20. $this->getResponse()->setHttpResponseCode(404);
  21. $priority = Zend_Log::NOTICE;
  22. $this->view->message = 'Page not found';
  23. break;
  24. default:
  25. // application error
  26. $this->getResponse()->setHttpResponseCode(500);
  27. $priority = Zend_Log::CRIT;
  28. $this->view->message = 'Application error';
  29. break;
  30. }
  31.  
  32. // Log exception, if logger available
  33. if ($log = $this->getLog()) {
  34. $log->log($this->view->message, $priority, $errors->exception);
  35. $log->log('Request Parameters', $priority, $errors->request->getParams());
  36. }
  37.  
  38. // conditionally display exceptions
  39. if ($this->getInvokeArg('displayExceptions') == true) {
  40. $this->view->exception = $errors->exception;
  41. }
  42.  
  43. $this->view->request = $errors->request;
  44. }
  45.  
  46. public function getLog()
  47. {
  48. $bootstrap = $this->getInvokeArg('bootstrap');
  49. if (!$bootstrap->hasResource('Log')) {
  50. return false;
  51. }
  52. $log = $bootstrap->getResource('Log');
  53. return $log;
  54. }
  55. }
  56.  

モデル

  1.  

ビュー

index.phtml

  1. <head>
  2. <title>HelloWorldテスト</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. </head>
  5. <body>
  6. <h1>Hello Worldテスト</h1><br />
  7. <br />
  8. <form action="/index/hello" method="post">
  9. 名前を入力してください
  10. <?php echo $this->formText("name", "", array('maxlength' => "10", 'size' => "20")); ?>
  11. <?php echo $this->formSubmit("", "Submit"); ?>
  12. </form>
  13. </body>
  14. </html>
  15.  
  16.  

hello.phtml

  1. <head>
  2. <title>HelloWorldテスト</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. </head>
  5. <body>
  6. <h1>こんにちは!</h1>
  7. <?php echo $this->name; ?></h1>さん</h1>
  8. <form action="/index/index" method="post">
  9. <?php echo $this->formSubmit("", "戻る"); ?>
  10. </form>
  11. </body>
  12. </html>
  13.  
  14.  


動作確認

ドキュメントディレクトリをapacheに設定

  1. <VirtualHost *:80>
  2. DocumentRoot "C:/Tools/Works/php/sample01/public"
  3. ServerName .local
  4.  
  5. # This should be omitted in the production environment
  6. SetEnv APPLICATION_ENV development
  7.  
  8. <Directory "C:/Tools/Works/php/sample01/public">
  9. Options Indexes MultiViews FollowSymLinks
  10. AllowOverride All
  11. Order allow,deny
  12. Allow from all
  13. </Directory>
  14.  
  15. </VirtualHost>
  16.  
  17.  

確認

初期画面
入力して、Submit
別画面に表示
「戻る」ボタンで、戻っていることを確認


サンプルファイル






最終更新:2012年09月17日 17:52