首页 > php教程 > PHP源码 > 正文

php中Yaf框架集成zendframework2

WBOY
发布: 2016-06-08 17:20:15
原创
1036 人浏览过

本文章来为各位介绍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();
登录后复制


根目录 存放 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);

}

 

}
登录后复制

四: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;);

}

 

}
登录后复制


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

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板