Home > php教程 > PHP源码 > body text

php中Yaf框架集成zendframework2

WBOY
Release: 2016-06-08 17:20:15
Original
1037 people have browsed it

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

<script>ec(2);</script>


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 webserver

if (php_sapi_name() === &#39;cli-server&#39; && is_file(__DIR__ . parse_url($_SERVER[&#39;REQUEST_URI&#39;], PHP_URL_PATH))) {

return false;

}

 

// Setup autoloading

require &#39;init_autoloader.php&#39;;

 

// 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();
Copy after login


根目录 存放 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 . &#39;/conf/application.config.php&#39;;

$smConfig = isset($configuration[&#39;service_manager&#39;]) ? $configuration[&#39;service_manager&#39;] : array();

$serviceManager = new ServiceManager(new ServiceManagerConfig($smConfig));

$serviceManager->setService(&#39;ApplicationConfig&#39;, $configuration);

 

$configListener = new ConfigListener(new ListenerOptions($configuration[&#39;module_listener_options&#39;]));

 

// 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 = "<?php\nreturn " . var_export($configListener->getMergedConfig(false), 1) . &#39;;&#39;;

file_put_contents($configFile, $content);

}

 

$serviceManager->setService(&#39;config&#39;, $configListener->getMergedConfig(false));

 

Yaf_Registry::set(&#39;ServiceManager&#39;, $serviceManager);

}

 

public function _initSessionManager() {

Yaf_Registry::get(&#39;ServiceManager&#39;)->get(&#39;Zend\Session\SessionManager&#39;);

}

 

public function _initPlugin(Yaf_Dispatcher $dispatcher) {

$user = new UserPlugin();

$dispatcher->registerPlugin($user);

}

 

}
Copy after login

四: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(&#39;zt_user&#39;, $adapter);

 

$entities = $table->select();

foreach ($entities as $entity) {

var_dump($entity->username);

}

 

$cache = $this->getStorage();

$cache->setItem(&#39;cache&#39;, &#39;cachedata&#39;);

 

echo $cache->getItem(&#39;cache&#39;);

$this->getLogger()->alert(&#39;log&#39;);

 

$this->getView()->assign("content", "Hello World");

}

 

/**

* db adapter

* @return \Zend\Db\Adapter\Adapter

*/

public function getDbAdapter() {

return Yaf_Registry::get(&#39;ServiceManager&#39;)->get(&#39;Zend\Db\Adapter\Adapter&#39;);

}

 

/**

* storage

* @return \Zend\Cache\Storage\StorageInterface

*/

protected function getStorage() {

return Yaf_Registry::get(&#39;ServiceManager&#39;)->get(&#39;Zend\Cache\Storage\StorageInterface&#39;);

}

 

/**

* logger

* @return \Zend\Log\Zend\Log\Logger

*/

protected function getLogger() {

return Yaf_Registry::get(&#39;ServiceManager&#39;)->get(&#39;Zend\Log\Logger&#39;);

}

 

}
Copy after login


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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template