本文章来为各位介绍php中Yaf框架集成zendframework2的例子,有兴趣的可以和一聚教程小编一起来看看,具体操作如下。
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() === '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 模块组件
vendor\ZF2 见页尾下载包
立即学习“PHP免费学习笔记(深入)”;
一款国产的,基于LGPL协议,开源免费的项目管理软件,它集产品管理、项目管理、测试管理于一体,同时还包含了事务管理、组织管理等诸多功能,是中小型企业项目管理的首选,基于自主的PHP开发框架──ZenTaoPHP而成,第三方开发者或企业可非常方便的开发插件或者进行定制。
312
三:更改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 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) . ';';
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字样
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号