`
amuropikin
  • 浏览: 41084 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Codeigniter整合PJAX(pushstate+ajax)实现无刷新页面

阅读更多
PJAX效果

通过url可以跟踪ajax的动态加载内容。这种效果尤其在two step view布局的视图中有很大的好处。无刷新加载页面,意味着响应速度和用户体验得到了极大的提升,在静态脚本和通用模块比较多的情况下,最大程度上节省了重用部分的开销。应用例子可以参考现在的g plus、facebook和新版微博,同样是基于html5的pushState实现。g plus的表现最为明显,点击导航栏地址,箭头随目标移动,同时加载的页面淡入,效果很炫。


使用CI+jQuery实现PJAX

不需要从头编写基于pushState的javascript插件,因为jQuery已有项目把它开源出来

开始前的准备:

1. jQuery librayhttp://code.jquery.com/jquery-1.8.2.min.js

2. 基于jQ的pjax插件(github上的项目)  https://github.com/defunkt/jquery-pjax

3. PHP项目代码(本文使用codeigniter,开发中大同小异)

实际开发中包括前端和后端代码,关于前端插件的使用方法可参考github上的说明

php端需要处理的任务主要是两件:1.实现layout视图布局  2.判断pjax过来的请求

layout使每个视图的输出都是在一个公用模版之上。

第一步是实现CI中的layout。在application/libraries文件夹中新建文件Layout.php, 加入如下代码:

<?php
/*

* --------------------------------------------------------------------
*  视图布局类
* --------------------------------------------------------------------
*
*  @authors ekin.cen <weibo.com/2839892994>

*/
class Layout{

    public $obj;
    public $disable_layout = FALSE;
    protected $_layout;
    protected $_layout_dir = 'layout';
    protected $_template_dir = 'templates';
    protected $_data = array();

    public function __construct()
    {
        $this->obj = &get_instance();
    }

    public function set_layout($layout)
    {

        $this->_layout = $layout;
        return $this;
    }

    public function set_layout_dir($dirname)
    {
        $this->_layout_dir = $dirname;
        return $this;
    }

    public function set_template_dir($dirname)
    {
        $this->_template_dir = $dirname;
        return $this;
    }

    public function view($view, $data = NULL, $return = FALSE)
    {
        $view = $this->_template_dir . DIRECTORY_SEPARATOR . $view;
        $this->_layout = $this->_layout_dir . DIRECTORY_SEPARATOR . $this->_layout;

        if ($this->_data)
        {
            $data = $data ? array_merge($this->_data, $data) : $this->_data;
        }
        if ($this->disable_layout)
        {
            echo $this->obj->load->view($view, $data, TRUE);
        }
        else
        {
            $data = array('TEMPLATE_CONTENT' => $this->obj->load->view($view, $data, TRUE));
            $this->obj->load->view($this->_layout,$data,$return);
        }
    }

    public function assign($key, $value = null)
    {
        $data = is_array($key) ? $key : array($key => $value);
        $this->_data = array_merge($data, $this->_data);
        return $this;
    }
}
/* End of file */

第二步是构建自定义的控制器超类,通过判断请求是否来自pjax,来决定是否启用layout进行输出. 在application/core下创建MY_Controller:

<?php
/*
*
* --------------------------------------------------------------------
*  控制器超类,加入pjax请求判断
* --------------------------------------------------------------------
*
*  @authors ekin.cen <weibo.com/2839892994>

*/
class MY_Controller extends CI_Controller{

        protected $_layout='layout_default';

        public function __construct(){
                parent::__construct();
                $this->_initialize();
        }

        protected function _initialize(){
                $this->_set_layout();
                // check if is pjax request
               if (array_key_exists('HTTP_X_PJAX', $_SERVER) && $_SERVER['HTTP_X_PJAX'])
               {
                   $this->layout->disable_layout = TRUE;
               }
        }

        protected function _set_layout(){
        $this->load->library('layout');
        //set template
        $this->layout->set_layout($this->_layout);
        }
}
/* End of file */


现在创建view下的模版文件和相关的controller来进行测试

demo 的 welcome controller.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

        /**
         * Index Page for this controller.
         *
         * Maps to the following URL
         *                 http://example.com/index.php/welcome
         *        - or - 
         *                 http://example.com/index.php/welcome/index
         *        - or -
         * Since this controller is set as the default controller in
         * config/routes.php, it's displayed at http://example.com/
         *
         * So any other public methods not prefixed with an underscore will
         * map to /index.php/welcome/<method_name>
         * @see http://codeigniter.com/user_guide/general/urls.html
         */
        public function index()
        {
                 $this->layout->view('index');
        }

        public function test1(){
                $this->layout->view('test1');
        }

        public function test2(){
                $this->layout->view('test2');
        }
}

/* End of file welcome.php */

最后在模版文件中输出内容,  详细的view文件可查看demo

打开支持html5的浏览器并观察网络请求,发现点击页面上的a标签会引起一个ajax请求,url跳转,而页面并没有重新载入,而且前进后退功能一切正常!

------------------------------------------------------------------

使用jquery-pjax插件需要注意的地方:

1 .返回的模版内容不能为纯文本,需要用html标签包裹

2.插件的使用方法,详情参考github的项目说明,可能因为版本问题和demo有所出入

3.对于不支持pushstate的低版本浏览器,pjax会自动判断或使用传统的页面加载模式

4.当一个页面的pjax请求时间过长也会导致插件使用刷新加载,这时需要设定插件内的超时时间

demo地址在ci论坛原帖

http://codeigniter.org.cn/forums/thread-14433-1-1.html
分享到:
评论

相关推荐

    CodeIgniter资料+ci+jquery范例

    CodeIgniter and Ajax using jQuery.zip (超赞的实例,代码非常简单) CodeIgniter+架构的说明教程.pdf CodeIgniter用户指南(版本1.7.2).chm CodeIgniter资料+ci+jquery范例.zip PHP 敏捷开发框架CodeIgniter.chm ...

    基于 Codeigniter 3.0.6 + Smarty 3 + AdminLTE + RBAC 的后台管理系统.zip

    php程序设计,web系统源码,源码,数据库MySQL,毕业设计项目,可用于课程设计作业等。

    CodeIgniter+用户指南+v+2.1.0.zip

    CI是一套给 PHP 网站开发者使用的应用程序开发...它提供一套丰富的标准库以及简单的接口和逻辑结构,其目的是使开发人员更快速地进行项目开发。使用CI可以减少代码的编写量,并将你的精力投入到项目的创造性开发上。

    Ajax-Codeigniter-3-Ajax-Form-Submission.zip

    Ajax-Codeigniter-3-Ajax-Form-Submission.zip,“codeigniter 3 ajax表单提交和验证教程”一集的源代码,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建...

    CodeIgniter 3 + Vue.js 3 + Vite和受支持的热模块替换(HMR)-PHP开发

    只是一个基本示例,说明如何将CodeIgniter 3 + Vue.js 3 + Vite与支持的热模块替换(HMR)集成。 CodeIgniter 3 + Vue.js 3 + Vite这只是一个基本示例,说明如何将CodeIgniter 3 + Vue.js 3 + Vite与受支持的热模块...

    Codeigniter框架整合Smarty引擎DEMO

    Codeigniter框架整合Smarty引擎DEMO

    CodeIgniter整合fck/ci整合fck实例一个

    CodeIgniter整合fck/ci整合fck实例一个,网上的许多资料在自己照着做的时候会发现有很多错误!所以自己做了个,ci版本是CodeIgniter_1.7.1的

    codeigniter-vue:CodeIgniter + VueJs-准备部署新项目

    “#codeigniter-vue”

    Ajax-online-chat-with-php-jquery-ajax-codeigniter.zip

    Ajax-online-chat-with-php-jquery-ajax-codeigniter.zip,这个应用程序表示使用codeigniter php框架、jquery和ajax的在线聊天。我还添加了表示用于测试应用程序的数据库的sql文件。只有首先导入数据库(chat.sql),...

    Ajax-JQuery-Ajax-POST-in-Codeigniter.zip

    Ajax-JQuery-Ajax-POST-in-Codeigniter.zip,如何将带有数据的ajax post请求发送到codeigniter控制器,而不刷新页面并将数据返回到视图。,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json...

    Ajax-codeigniter-ajax-crud.zip

    Ajax-codeigniter-ajax-crud.zip,使用codeigniter、jquery和ajax实现简单的crud,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小...

    Ajax-codeigniter-voting.zip

    Ajax-codeigniter-voting.zip,投票系统php-ajax教程,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和...它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下更改。

    Ajax-CodeIgniter-Ajax-Search.zip

    Ajax-CodeIgniter-Ajax-Search.zip,codeigniter ajax实时搜索,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页...

    Ajax-codeigniter-stripe.zip

    Ajax-codeigniter-stripe.zip,codeigniter 3.1.10中的简单条带支付网关集成(带bootstrap 4),ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,...

    Laravel+Codeigniter+NodeJS+HTML 后台管理模板

    Codeigniter 入门项目 NodeJS 入门项目 令人惊叹的仪表板页面布局 使用 Bootstrap 4 构建 干净的代码 基于组件的结构 悬停效果 谷歌地图 易于定制 独特、干净和现代的设计 完全响应 跨浏览器优化 使用免费的 Google ...

    CodeIgniter框架实现的整合Smarty引擎DEMO示例

    主要介绍了CodeIgniter框架实现的整合Smarty引擎DEMO,结合实例形式分析了CodeIgniter框架整合Smarty引擎的原理、操作步骤及相关实现技巧,需要的朋友可以参考下

    ucloud codeigniter sdk整合插件

    ucloud codeigniter sdk整合插件

    php基于CodeIgniter简易产品后台管理Demo+数据库

    php基于CodeIgniter简易产品后台管理Demo+数据库 环境要求 WAMPserver 注意事项 Apache httpd-vhosts.conf注册ServerName pro_product.loc 导入product_db.sql 更改CI的database文件'database' =&gt; 'product_db'及本...

    Codeigniter里的无刷新上传的实现代码

    说是codeigniter里的无刷新上传吧,fashion 一点的说法就是利用AJAX技术上传。其中用到了Jquery和 AjaxFileUpload 。 先建个表 CREATE TABLE `files` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `filename...

    CodeIgniter and Ajax using jQuery.zip

    CodeIgniter and Ajax using jQuery.zip

Global site tag (gtag.js) - Google Analytics