zoukankan      html  css  js  c++  java
  • Zend framework

    Zend framework框架
    开发web程序和服务,ZF采用MVC架构模式来分离应用程序下。
    PHP5面向对象描写:丰富完善的组件支持,Ajax支持

    OOP MVC

    环境需求和配置
    php版本需要在5.2.0以上(Wamp)

    php需要开启PDO和PDO相关数据库引擎来运行ZF:extension=php_pdo.dll之前的分号去掉

    Apache 开启rewrite_module模块,并开启.htaccess目录AllowOveride ALL

    ZF框架的搭配与调试:
    http://www.zendframework.com/download/latest
    ------application MVC文件夹
    ------------controllers 控制文件夹
    ------------models 模块文件夹
    ------------views 模板文件夹
    ----------------filters
    ----------------helpers
    ----------------scripts
    ------library
    -------------Zend
    -----public 公共配置文件夹
    -------------images
    -------------scripts
    -------------styles
    -----.htaccess
    -----.project
    -----index.php

    index.php 配置文件,单一入口。

    可以下载一个Zend Framework的核心板,然后复制出library文件夹
    init()代替__construct,进行优化,使用更加方便

    Index控制器中 $this->view->word = '测试一个内容';
    ========================================
    php100:76:Zend Framework 数据库操作

    Zend framework 如何写数据库配置文件
    在application 文件夹下面创建一个config文件夹,然后建立一个config.ini文件,如下图所示:
    [general]
    db.adapter=PDO_MYSQL
    db.config.host=localhost
    db.config.username=root
    db.config.password=
    db.config.dbname=zend

    配置文件引入到Zend Framework
    $config=new Zend_Config_ini('./application/config/config.ini',null,true);
    Zend_Registry::set('config',$config);

    $dbAdapter=Zend_Db::factory($config->general->db->adapter,
    $config->general->db->config->toArray());

    如何控制和处理Zend Framework方法内容
    采用单一入口模式,所有后面跟的参数需要 /来区别
    localhost/zendframework/index/add
    function addAction(){}
    IndexActionController.php
    通过这样的方式访问了index模块下的add方法,系统默认的是indexAction()索引方法名。

    =================================
    php100:77:Zend Framework数据库之修改和视图函数

    Zend Framework Db_Table常用函数介绍:
    fetchAll($sql):取回结果集中所有字段的值,作为连续数组返回
    fetchRow($sql):只返回结果集中的一行
    fetchAssoc($sql):取回结果集中所有字段的值,作为关联数组返回
    fetchCol($sql):取回所有结果航的第一个字段名
    fetchOne($sql):只取回第一个字段值
    fetchPairs($sql):取回相关数组,第一个是Key值,第二个是值

    insert($arrParams)
    关联数组,key是数据库字段
    update($arrParams,$steSqlWhere)
    delete($steSqlWhere)

    insert()插入数据
    insert()方法,列名:数据的关联数组作为参数,自动加引号处理,并返回插入的最后一行的id值
    $data=array{
    'name'=>'King';
    'color'=>'blue';
    }
    $id=$table->insert($data);

    update()
    方法,列名;数据的关联数组作为参数。
    $table=new RoundTable();
    $db=$table->getAdapter();
    $set=array{
    'color'=>'yellow';
    }
    $where =$db->quoteInto('first_name=?','Robin');
    $table->update($set,$where);

    delete()
    $where=$db->quoteInto('first_name=?','Robin');
    $table->delete($where);


    Zend Framework视图循环和条件函数:
    Foreach 循环
    Foreach(循环条件) :....endforeach;
    IF
    if()......endif;

     =======================================

    MVC 代码书写:
    控制器代码书写:
    <?php
    class IndexController extends Zend_Controller_Action
    {
    function init()
    {
    $this->registry = Zend_Registry::getInstance();
    $this->view = $this->registry['view'];
    $this->view->baseUrl = $this->_request->getBaseUrl();

    }
    function indexAction()
    {
    $this->view->word=" I love spurs";

    echo $this->view->render("index.html");

    }
    function addAction(){
    //如果是POST过来的值.就增加.否则就显示增加页面


    }
    }
    ?>
    控制当中写内容:$this->view->word="ggg";
    $this->view->render("index.html");
    ---->index.html echo $this->word;

    application->config.ini
    [general]
    db.adapter=PDO_MYSQL
    db.config.host=localhost
    db.config.username=root
    db.config.password=
    db.config.dbname=think_zw

    配置文件引入到framework里面去
    //配置数据库参数,并连接数据库
    $config=new Zend_Config_Ini('./application/config/config.ini',null, true);
    Zend_Registry::set('config',$config);
    $dbAdapter=Zend_Db::factory($config->general->db->adapter,$config->general->db->config->toArray());
    $dbAdapter->query('SET NAMES UTF8');
    Zend_Db_Table::setDefaultAdapter($dbAdapter);
    Zend_Registry::set('dbAdapter',$dbAdapter);

    单一入口模式:localhost/index/add/访问index模块下的add方法
    function addAction(){}(在IndexController.php)
    默认访问为index模块下的index方法

    再建立一个模块model里面的message.php
    <?php
    class Message extends Zend_Db_Table
    {
    protected $_name ="message";
    protected $_primary = 'id';
    }
    ?>
    模块实例化:
    function indexAction()
    {
    $message=new message();//实例化数据库类

    //获取数据库内容
    $this->view->messages=$message->fetchAll()->toArray();

    echo $this->view->render('index.phtml');//显示模版
    }

    <?foreach($this->messages as $message): ?>
    <tr>
    <th><?php echo $message['title']; ?></th>
    <td><?php echo $message['content']; ?></td>
    </tr>
    <?endforeach; ?>


    *************
    修改和删除数据

    <?php if(2==2):?>
    kk
    <?php else:?>
    ll
    <?php endif;?>

    index.phtml里面加上<a href="<?php echo $this->baseUrl?>/index/exit">编辑</a>
    <a href="<?php echo $this->baseUrl?>/index/delete">删除</a>

    添加一个新的方法:edit.phtml
    function editAction(){

    $message = new Message();
    $db = $message->getAdapter();

    if(strtolower($_SERVER['REQUEST_METHOD'])=='post'){
    $id = $this->_request->getPost('id');
    $cid = $this->_request->getPost('cid');
    $title = $this->_request->getPost('title');

    $set = array(
    'cid'=>$cid,
    'title'=>$title
    );
    $where = $db->quoteInto('id = ?',$id);
    //更新数据
    $message->update($set,$where);
    unset($set);
    echo '修改数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
    }else{
    $id = $this->_request->getParam('id');
    $this->view->messages = $message->fetchAll('id='.$id)->toArray();
    echo $this->view->render('edit.phtml');
    }
    }


    function delAction(){
    $message = new Message();
    $id = (int)$this->_request->getParam('id');

    if($id > 0){
    $where = 'id = ' . $id;
    $message->delete($where);
    }
    echo '删除数据成功!<a href="'.$this->view->baseUrl.'/index/index/">返回</a>';
    }


    异常出现:
    Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (index.php)' in

    解决办法:在index.php中的
    $frontController =Zend_Controller_Front::getInstance();后加上
    $frontController->setParam('useDefaultControllerAlways', true);

    *******
    id/3 等于以前的?id=3

  • 相关阅读:
    springcloud中常用的注解
    MySQL--Profiling和Trace使用
    MySQL Execution Plan--IN查询计划
    MySQL Config--参数system_time_zone和参数time_zone
    MySQL Replication--全局参数gtid_executed和gtid_purged
    MySQL Transaction--事务无法正常回滚导致的异常
    MySQL Execution Plan--数据排序操作
    MySQL Session--批量KILL会话
    MySQL Transaction--MySQL与SQL Server在可重复读事务隔离级别上的差异
    MySQL Transaction--事务相关查询
  • 原文地址:https://www.cnblogs.com/smartyman/p/3799690.html
Copyright © 2011-2022 走看看