加入收藏 | 设为首页 | 会员中心 | 我要投稿 汕尾站长网 (https://www.0660zz.cn/)- 研发安全、行业智能、图像分析、视频终端、数据迁移!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php中Yaf框架集成zendframework2

发布时间:2022-07-04 09:56:57 所属栏目:PHP教程 来源:互联网
导读:本文章来为各位介绍php中Yaf框架集成zendframework2的例子,有兴趣的可以和php粉丝网教程小编一起来看看,具体操作如下. php框架 Yaf集成zendframework2,zf2的orm 可以作为独立模块用到yaf中,而且zf2 composer service manger cacheStorage 都可以集成到yaf中.
  本文章来为各位介绍php中Yaf框架集成zendframework2的例子,有兴趣的可以和php粉丝网教程小编一起来看看,具体操作如下.
 
  php框架 Yaf集成zendframework2,zf2的orm 可以作为独立模块用到yaf中,而且zf2 composer service manger  cacheStorage 都可以集成到yaf中.
 
  一:publicindex.php 加入composer
 
  chdir(dirname(__DIR__));
   
  // Decline static file <a href="/tags.php/request/" target="_blank">request</a>s back to the PHP built-in webserver
   
  if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
   
  return false;
   
  }
   
  // Setup autoloading
   
  require 'init_autoloader.php';
   
   
  // Define path to application directory
   
  define("APP_PATH", dirname(__DIR__));
   
   
  // Create application, bootstrap, and run
   
  $app = new Yaf_Application(APP_PATH . "/conf/application.ini");
   
  $app->bootstrap()->run();
  根目录 存放 init_autoloader.php
 
  二:导入ZF2 模块组件
 
  vendorZF2  见页尾下载包
 
  三:更改bootstrap配置文件
 
  <?php
  use ZendServiceManagerServiceManager;
   
  use ZendMvcServiceServiceManagerConfig;
   
  use ZendModuleManagerListenerConfigListener;
   
  use ZendModuleManagerListenerListenerOptions;
   
  use ZendModuleManagerModuleEvent;
   
  class Bootstrap extends Yaf_Bootstrap_Abstract {
   
  public function _initConfig() {
   
  $config = Yaf_Application::app()->getConfig();
   
  Yaf_Registry::set("config", $config);
   
  }
   
  public function _initServiceManager() {
   
  $configuration = require APP_PATH . '/conf/application.config.php';
   
  $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
   
  $serviceManager = new ServiceManager(new ServiceManagerConfig($smConfig));
   
  $serviceManager->setService('ApplicationConfig', $configuration);
   
  $configListener = new ConfigListener(new ListenerOptions($configuration['module_listener_options']));
   
  // If not found cache, merge config
   
  if (!$configListener->getMergedConfig(false)) $configListener->onMergeConfig(new ModuleEvent);
   
  // If enabled, update the config cache
   
  if ($configListener->getOptions()->getConfigCacheEnabled() &&
   
  !file_exists($configListener->getOptions()->getConfigCacheFile())) {
   
  //echo "debug";
   
  $configFile = $configListener->getOptions()->getConfigCacheFile();
   
  $content = "<?phpnreturn " . var_export($configListener->getMergedConfig(false), 1) . ';';
   
  file_put_contents($configFile, $content);
   
  }
   
  $serviceManager->setService('config', $configListener->getMergedConfig(false));
   
  Yaf_Registry::set('ServiceManager', $serviceManager);
   
  }
   
  public function _initSessionManager() {
   
  Yaf_Registry::get('ServiceManager')->get('ZendSessionSessionManager');
   
  }
   
  public function _initPlugin(Yaf_Dispatcher $dispatcher) {
   
  $user = new UserPlugin();
   
  $dispatcher->registerPlugin($user);
   
  }
   
  }
  四:mvc测试
 
  <?php
  use ZendSessionContainer as SessionContainer;
   
  use ZendDbTableGatewayTableGateway;
   
  class IndexController extends Yaf_Controller_Abstract {
   
  public function indexAction() {
   
  $adapter = $this->getDbAdapter();
   
  $table = new TableGateway('zt_user', $adapter);
   
  $entities = $table-><a href="/tags.php/select/" target="_blank">select</a>();
   
  <a href="/tags.php/foreach/" target="_blank">foreach</a> ($entities as $entity) {
   
  var_dump($entity->username);
   
  }
   
  $cache = $this->getStorage();
   
  $cache->setItem('cache', 'cachedata');
   
  echo $cache->getItem('cache');
   
  $this->getLogger()->alert('log');
   
  $this->getView()->assign("content", "Hello World");
   
  }
   
  /**
   
  * db adapter
   
  * @return ZendDbAdapterAdapter
   
  */
   
  public function getDbAdapter() {
   
  return Yaf_Registry::get('ServiceManager')->get('ZendDbAdapterAdapter');
   
  }
   
  /**
   
  * storage
   
  * @return ZendCacheStorageStorageInterface
   
  */
   
  protected function getStorage() {
   
  return Yaf_Registry::get('ServiceManager')->get('ZendCacheStorageStorageInterface');
   
  }
   
  /**
   
  * logger
   
  * @return ZendLogZendLogLogger
   
  */
   
  protected function getLogger() {
   
  return Yaf_Registry::get('ServiceManager')->get('ZendLogLogger');
   
  }
   
  }
  这样你访问public下的index.php 会输出hello word字样.

(编辑:汕尾站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读