【问题标题】:CakePHP - jQuery Mobile returns empty page containing the word "Undefined"CakePHP - jQuery Mobile 返回包含单词“未定义”的空白页面
【发布时间】:2014-04-07 22:12:56
【问题描述】:

我正在尝试在 JQM 中编写一个 Cake 站点,但是每当我点击一个链接时,屏幕就会跳转到一个只包含“未定义”一词的页面。

检查源代码发现有一个新的 div,其中 data-role="page" 包含这个词,并且我的旧页面的原始内容保持不变。

这是我的移动视图布局:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title><?php echo (isset($title_for_layout)) ? $title_for_layout : 'My default title';?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <?php
            // javascript
            $javascripts = array(
                    'jquery',
                    'jquery.mobile-1.0.min.js',
                );
            echo $this->Html->script($javascripts);
            // css
            $css = array('jquery.mobile-1.0.css','jquery.mobile.structure-1.0.css','themes/mobires.css');
            echo $this->Html->css($css) . "\n\t";
        ?>
    </head>
    <body>
        <div data-role="page">
            <?php echo $this->Session->flash(); ?>
            <div data-role="header">
                <h1>
                    <?php echo (isset($title_for_layout)) ? $title_for_layout : 'My default title';?>
                </h1>
                 <div data-role="navbar">
                    <ul>
                        <li><?php echo $this->Html->link('Home',array('controller'=>'pages','action'=>'display','home'))?></li>
                        <li><?php echo $this->Html->link('Contact',array('controller'=>'pages','action'=>'display','contact'))?></li>
                    </ul>
                </div><!-- /navbar -->
            </div><!-- /header -->
            <div data-role="content">
                <?php
                    echo $this->Session->flash();
                    echo $content_for_layout;
                ?>
            </div>
            <!-- start footer -->
                <div data-role="footer">
                    &copy; 2012<?php if ( date("Y") > 2012 ) { echo '-' . date("Y"); } ?> Mobires.  All Rights Reserved.
                    <br />
                    &nbsp;
                    <?php echo $this->Html->link('Privacy Policy',array('controller'=>'pages','action'=>'display','privacy'))?>
                    &nbsp;
                    <?php echo $this->Html->link('Terms & Conditions',array('controller'=>'pages','action'=>'display','terms'))?>

                </div>
        </div><!-- /page -->
        <?php echo $this->Js->writebuffer(); ?>
    </body>
</html>

而我的 home.ctp 中只有“test”这个词。我正在尝试单击页脚中的按钮来测试 Ajax 加载。

【问题讨论】:

    标签: jquery cakephp mobile


    【解决方案1】:

    如果您想将 cakephp 与 jquery mobile 集成,我认为这是最好的解决方案。也解决了 cakephp 2.x 上漏掉的空页。您还可以看到一个示例,说明如何使用 Inflector::underscore($this->action) 在 cakephp 控制器中获取视图文件(渲染文件)的名称。

    public function beforeFilter() {
    $this->isMobile = false;
            if ($this->RequestHandler->isMobile()) {
    
                // required for CakePHP 2.2.2
                $viewDir = App::path('View');
                // returns an array
                /*
                 * array(
                 *      (int) 0 => '/var/www/maps-cakephp2/app/View/'
                 * ) 
                 */
                 //path to your folder for mobile views . You must modify these lines that you want
                 //in my case I have views in folders in to /app/view/mobile and here users folder etc.
                $mobileView = $viewDir[0] .
                        'mobile' . DS . $this->name . DS;
                $mobileViewFile = $mobileView .
                        Inflector::underscore($this->action) . '.ctp';
    
                if (file_exists($mobileViewFile)) {
                    $this->isMobile = true;
                    $this->viewPath = 'mobile' . DS . $this->name;
    
                }
    
        }
    }
    
    public function beforeRender() {
        if ($this->isMobile == true) {
            $this->autorender = true;
            //app/View/Layouts/mobile.ctp
            $this->layout = 'mobile';
        }
    }
    

    【讨论】:

    • 您还可以在 cakephp 中为视图使用新的主题功能
    【解决方案2】:

    啊,JQM 需要一个完全格式化的 JQM 响应,因此必须返回整个“页面”划分,而不仅仅是内容。

    【讨论】:

    • 嗨,安迪。我有同样的问题......你是如何在 CakePHP 中获得完全格式化的 JQM 响应???
    • 好的,我在这里找到了解决方案:stackoverflow.com/questions/6385714/cakephp-ajax-layout
    • 我只是通过声明我在内容之前和之后输出的“页眉”和“页脚”元素来添加 $content_for_layout 中缺少的 JQM 响应的位。关闭请求处理程序对您有何帮助?
    • 我在我的 AppController::beforeFilter()-function $this->RequestHandler->enabled = false; class AppController extends Controller { var $components = array('RequestHandler'); function beforeFilter() { if ($this-&gt;RequestHandler-&gt;isMobile()) { $this-&gt;RequestHandler-&gt;enabled = false //set special mobile rules here } } }
    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多