php框架Yaf 集成ZendFramework DB 澳门新葡亰网址大全

2019-09-30 作者:网络时代   |   浏览(173)

[product]
 
;layout
application.directory = APP_PATH
application.bootstrap = APP_PATH "Bootstrap.php"
application.library = APP_PATH "../library"
 
;app
;application.baseUri = ''
;application.dispatcher.defaultModule = index
application.dispatcher.defaultController = index
application.dispatcher.defaultAction = index
 
;errors (see Bootstrap::initErrors)
application.showErrors=1
 
;enable the error controller
application.dispatcher.catchException=1
 
;database
database.adapter = pdo_mysql
;database.params.dbname=APP_PATH "/db/application"
database.params.dbname = "yof_dym"
database.params.host = "127.0.0.1"
database.params.username = "root"
database.params.password = "root"
 
[devel : product]
 
;errors (see Bootstrap::initErrors)
application.showErroes=1

2、首先进入cmd,命令行窗口模式。
直接打下面的命令,
C:Documents and SettingsAdministrator>E:PHP开源框架ZendFramework-1.11.3ZendFramework-1.11.3binzf.bat create project D:tempzflearn
3、C:Documents and SettingsAdministrator>d:  
进入D盘
D:>cd tempzflearn
进入网站的目录
D:tempzflearn>E:PHP开源框架ZendFramework-1.11.3ZendFramework-1.11.3binzf.bat create module vote
创建vote模型
在D:tempzflearnapplicationmodulesvotecontrollers下创建LoginController.php控制器
同时也在D:webzfapplicationmodulesvoteviewsscripts下生成视图 loginindex.phtml
D:tempzflearn>E:PHP开源框架ZendFramework-1.11.3ZendFramework-1.11.3binzf.bat create controller login 1 vote
如果后面不加1 vote
则创建在D:tempzflearnapplicationcontrollers
D:tempzflearnapplicationviewsscripts下生成视图 loginindex.phtml
拷贝zend库
将E:PHP开源框架ZendFramework-1.11.3ZendFramework-1.11.3library 下的zend文件夹拷贝到
在D:tempzflearnlibrary下

根目录 存放 init_autoloader.php

附:网站整个目录不能有空格不然生成不成功

php框架 Yaf集成zendframework2, zf2的orm 可以作为独立模块用到yaf中,而且zf2 composer service manger  cacheStorage 都可以集成到yaf中。

Bootstrap.php

php框架 Yaf集成zendframework2

/** Zend_Application */
require_once 'Zend/Application.php';

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();

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/application'));
defined('WEBROOT_PATH')
    || define('WEBROOT_PATH', realpath(dirname(__FILE__)));

四:mvc测试

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

_initIncludePath自动加载zend类文件

<?php

CREATE TABLE IF NOT EXISTS `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`centent` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

<?php
class IndexController extends Yaf_Controller_Abstract {
 
private $_layout;
 
public function init(){
$this->_layout = Yaf_Registry::get('layout');
}
/**
* 主页面
*/
public function indexAction() {
 
$blog = new BlogModel();
/*view*/
$entries = $blog->fetchAll();
 
print_r($entries);
$this->_view->entries = $entries;
$this->_view->rowsNum = count($entries)+1;
 
/*layout*/
$this->_layout->meta_title = 'blog';
}
/**
* 增加
*/
public function addAction(){
if ($_POST){
$bind = array(
'name'=>$_POST['msg_name'],
'content'=>$_POST['msg_content']
);
$blog = new BlogModel();
$result = $blog->add($bind);
if ($result){
$this->_view->msg = "增加成功";
}else{
$this->_view->msg = "增加失败";
}
}
}
/**
* 编辑
*/
public function editAction() {
if ($_POST){
$bind = array(
'id' => $_POST['msg_id'],
'name' => $_POST['msg_name'],
'content' => $_POST['msg_centent']
);
$blog = new BlogModel();
$result = $blog->edit($bind, $bind['id']);
if ($result){
$this->_view->msg = "修改成功";
}else{
$this->_view->msg = "修改失败";
}
}else {
$blog = new BlogModel();
$id = $this->getRequest()->getParam('id');
$msgOne = $blog->getMsgByid($id);
$this->_view->msgOne = $msgOne;
}
}
/**
* 删除
*/
public function deleAction(){
$blog = new BlogModel();
$id = $this->getRequest()->getParam('id');
$result = $blog->delete("`id` = '$id'");
if ($result){
echo "删除成功";
}else{
echo "删除失败";
}
exit();
}
}

application.ini

二:定义配置文件application.ini

?>

vendorZF2 http://pan.baidu.com/s/1dDfL5RF

[testing : production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

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

index.php代码

三:更改Bootstrap启动文件

1、下载zf最新版Zend Framework- Downloads- Downloads

本文由澳门新葡亰网址大全发布于网络时代,转载请注明出处:php框架Yaf 集成ZendFramework DB 澳门新葡亰网址大全

关键词: