php中Yaf框架組成zendframework2

來源:互聯網
上載者:User


php架構 Yaf整合zendframework2, zf2的orm 可以作為獨立模組用到yaf中,而且zf2 composer service manger  cacheStorage 都可以整合到yaf中。

一:public\index.php 加入composer

chdir(dirname(__DIR__)); // Decline static file requests back to the PHP built-in webserverif (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {return false;} // Setup autoloadingrequire 'init_autoloader.php'; // Define path to application directorydefine("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 模組組件

vendor\ZF2  見頁尾下載包

三:更改bootstrap設定檔

<?php use Zend\ServiceManager\ServiceManager;use Zend\Mvc\Service\ServiceManagerConfig;use Zend\ModuleManager\Listener\ConfigListener;use Zend\ModuleManager\Listener\ListenerOptions;use Zend\ModuleManager\ModuleEvent; 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 configif (!$configListener->getMergedConfig(false)) $configListener->onMergeConfig(new ModuleEvent); // If enabled, update the config cacheif ($configListener->getOptions()->getConfigCacheEnabled() &&!file_exists($configListener->getOptions()->getConfigCacheFile())) {//echo "debug";$configFile = $configListener->getOptions()->getConfigCacheFile();$content = "<?php\nreturn " . 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('Zend\Session\SessionManager');} public function _initPlugin(Yaf_Dispatcher $dispatcher) {$user = new UserPlugin();$dispatcher->registerPlugin($user);} }

四:mvc測試

<?php use Zend\Session\Container as SessionContainer;use Zend\Db\TableGateway\TableGateway; class IndexController extends Yaf_Controller_Abstract { public function indexAction() { $adapter = $this->getDbAdapter(); $table = new TableGateway('zt_user', $adapter); $entities = $table->select();foreach ($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 \Zend\Db\Adapter\Adapter*/public function getDbAdapter() {return Yaf_Registry::get('ServiceManager')->get('Zend\Db\Adapter\Adapter');} /*** storage* @return \Zend\Cache\Storage\StorageInterface*/protected function getStorage() {return Yaf_Registry::get('ServiceManager')->get('Zend\Cache\Storage\StorageInterface');} /*** logger* @return \Zend\Log\Zend\Log\Logger*/protected function getLogger() {return Yaf_Registry::get('ServiceManager')->get('Zend\Log\Logger');} }


這樣你訪問public下的index.php 會輸出hello word字樣

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.