Yaf framework integration zendframework2-PHP source code in php

Source: Internet
Author: User
Tags php framework
This article will introduce you to the example of Yaf framework integrating zendframework2 in php. If you are interested, you can take a look at it together with the tutorials. The specific operations are as follows. This article will introduce you to the example of Yaf framework integrating zendframework2 in php. If you are interested, you can take a look at it together with the tutorials. The specific operations are as follows.

Script ec (2); script


The php framework Yaf integrates zendframework2. The zf2 orm can be used as an independent module in yaf, and zf2 composer service manger cacheStorage can be integrated into yaf.

I. Add public \ index. php to 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();


Store init_autoloader.php in the root directory

Ii. Import ZF2 module components

For more information, see downloading packages at the end of the page.

3. Change the bootstrap configuration file

 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 = "
 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);} }

Iv. mvc Testing

 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');} }


In this way, the index. php under public will output the hello word.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.