当前位置:首页 > PHP教程 > php高级应用 > 列表

php中Yaf框架集成zendframework2

发布:smiling 来源: PHP粉丝网  添加日期:2016-01-20 16:10:45 浏览: 评论:0 

本文章来为各位介绍php中Yaf框架集成zendframework2的例子,有兴趣的可以和php粉丝网教程小编一起来看看,具体操作如下.

php框架 Yaf集成zendframework2,zf2的orm 可以作为独立模块用到yaf中,而且zf2 composer service manger  cacheStorage 都可以集成到yaf中.

一:public\index.php 加入composer

  1. chdir(dirname(__DIR__)); 
  2.  
  3. // Decline static file <a href="/tags.php/request/" target="_blank">request</a>s back to the PHP built-in webserver 
  4.  
  5. if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) { 
  6.  
  7. return false; 
  8.  
  9. } 
  10.  
  11. // Setup autoloading 
  12.  
  13. require 'init_autoloader.php'; 
  14.  
  15.  
  16. // Define path to application directory 
  17.  
  18. define("APP_PATH", dirname(__DIR__)); 
  19.  
  20.  
  21. // Create application, bootstrap, and run 
  22.  
  23. $app = new Yaf_Application(APP_PATH . "/conf/application.ini"); 
  24.  
  25. $app->bootstrap()->run(); 

根目录 存放 init_autoloader.php

二:导入ZF2 模块组件

vendor\ZF2  见页尾下载包

三:更改bootstrap配置文件

  1. <?php 
  2. use Zend\ServiceManager\ServiceManager; 
  3.  
  4. use Zend\Mvc\Service\ServiceManagerConfig; 
  5.  
  6. use Zend\ModuleManager\Listener\ConfigListener; 
  7.  
  8. use Zend\ModuleManager\Listener\ListenerOptions; 
  9.  
  10. use Zend\ModuleManager\ModuleEvent; 
  11.  
  12. class Bootstrap extends Yaf_Bootstrap_Abstract { 
  13.  
  14. public function _initConfig() { 
  15.  
  16. $config = Yaf_Application::app()->getConfig(); 
  17.  
  18. Yaf_Registry::set("config", $config); 
  19.  
  20. } 
  21.  
  22. public function _initServiceManager() { 
  23.  
  24. $configuration = require APP_PATH . '/conf/application.config.php'; 
  25.  
  26. $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array(); 
  27.  
  28. $serviceManager = new ServiceManager(new ServiceManagerConfig($smConfig)); 
  29.  
  30. $serviceManager->setService('ApplicationConfig', $configuration); 
  31.  
  32. $configListener = new ConfigListener(new ListenerOptions($configuration['module_listener_options'])); 
  33.  
  34. // If not found cache, merge config 
  35.  
  36. if (!$configListener->getMergedConfig(false)) $configListener->onMergeConfig(new ModuleEvent); 
  37.  
  38. // If enabled, update the config cache 
  39.  
  40. if ($configListener->getOptions()->getConfigCacheEnabled() && 
  41.  
  42. !file_exists($configListener->getOptions()->getConfigCacheFile())) { 
  43.  
  44. //echo "debug"; 
  45.  
  46. $configFile = $configListener->getOptions()->getConfigCacheFile(); 
  47.  
  48. $content = "<?php\nreturn " . var_export($configListener->getMergedConfig(false), 1) . ';'; 
  49.  
  50. file_put_contents($configFile, $content); 
  51.  
  52. } 
  53.  
  54. $serviceManager->setService('config', $configListener->getMergedConfig(false)); 
  55.  
  56. Yaf_Registry::set('ServiceManager', $serviceManager); 
  57.  
  58. } 
  59.  
  60. public function _initSessionManager() { 
  61.  
  62. Yaf_Registry::get('ServiceManager')->get('Zend\Session\SessionManager'); 
  63.  
  64. } 
  65.  
  66. public function _initPlugin(Yaf_Dispatcher $dispatcher) { 
  67.  
  68. $user = new UserPlugin(); 
  69.  
  70. $dispatcher->registerPlugin($user); 
  71.  
  72. } 
  73.  
  74. } 

四:mvc测试

  1. <?php 
  2. use Zend\Session\Container as SessionContainer; 
  3.  
  4. use Zend\Db\TableGateway\TableGateway; 
  5.  
  6. class IndexController extends Yaf_Controller_Abstract { 
  7.  
  8. public function indexAction() { 
  9.  
  10. $adapter = $this->getDbAdapter(); 
  11.  
  12. $table = new TableGateway('zt_user', $adapter); 
  13.  
  14. $entities = $table-><a href="/tags.php/select/" target="_blank">select</a>(); 
  15.  
  16. <a href="/tags.php/foreach/" target="_blank">foreach</a> ($entities as $entity) { 
  17.  
  18. var_dump($entity->username); 
  19.  
  20. } 
  21.  
  22. $cache = $this->getStorage(); 
  23.  
  24. $cache->setItem('cache', 'cachedata'); 
  25.  
  26. echo $cache->getItem('cache'); 
  27.  
  28. $this->getLogger()->alert('log'); 
  29.  
  30. $this->getView()->assign("content", "Hello World"); 
  31.  
  32. } 
  33.  
  34. /** 
  35.  
  36. * db adapter 
  37.  
  38. * @return \Zend\Db\Adapter\Adapter 
  39.  
  40. */ 
  41.  
  42. public function getDbAdapter() { 
  43.  
  44. return Yaf_Registry::get('ServiceManager')->get('Zend\Db\Adapter\Adapter'); 
  45.  
  46. } 
  47.  
  48. /** 
  49.  
  50. * storage 
  51.  
  52. * @return \Zend\Cache\Storage\StorageInterface 
  53.  
  54. */ 
  55.  
  56. protected function getStorage() { 
  57.  
  58. return Yaf_Registry::get('ServiceManager')->get('Zend\Cache\Storage\StorageInterface'); 
  59.  
  60. } 
  61.  
  62. /** 
  63.  
  64. * logger 
  65.  
  66. * @return \Zend\Log\Zend\Log\Logger 
  67.  
  68. */ 
  69.  
  70. protected function getLogger() { 
  71.  
  72. return Yaf_Registry::get('ServiceManager')->get('Zend\Log\Logger'); 
  73.  
  74. } 
  75.  
  76. } 

这样你访问public下的index.php 会输出hello word字样.

Tags: Yaf框架 zendframework2

分享到: