简体   繁体   English

CakePHP AJAX布局

[英]CakePHP AJAX Layout

I'm working with a CakePHP application and jQuery Mobile. 我正在使用CakePHP应用程序和jQuery Mobile。 In the CakePHP application, RequestHandler is turned on, now, jQuery Mobile makes all of it's requests as ajax requests but requires a full page not just what is in my view but the full layout to. 在CakePHP应用程序中,RequestHandler已打开,现在,jQuery Mobile将所有请求作为ajax请求发出,但不仅需要整页,而且需要完整的页面布局。

I need requesthandler and I've tried to set a layout, force a render, turn off autolayout, nothing works, cake only returns the contents of my view. 我需要requesthandler,并且尝试设置布局,强制渲染,关闭自动布局,什么都没有用,cake只返回我视图的内容。

I'd really love to know what's going on here. 我真的很想知道这是怎么回事。

If anyone is interested I found a solution to this, I found out that when you have RequestHandler on and make a Ajax request, it doesn't matter what you do, RequestHandler then decides that your layout is 'ajax' via call backs, this probably applies for all non-html request types, like json and text. 如果有人感兴趣,我找到了解决方案,我发现当您打开RequestHandler并发出Ajax请求时,无论您做什么都没有关系,然后RequestHandler通过回调确定您的布局为“ ajax”,这可能适用于所有非HTML请求类型,例如json和text。

I had to set 我必须设定

$this->RequestHandler->enabled = false; 

It really needs to be set in beforeFilter() as well, latter in the call chain and it appears to not work. 确实也需要在调用链中的beforeFilter()中设置它,它似乎不起作用。

So my code ended up as: 所以我的代码最终变成:

class AppController extends Controller {
  var $components = array('RequestHandler');
  function beforeFilter() {
    if ($this->RequestHandler->isMobile()) {
      $this->RequestHandler->enabled = false
      //set special mobile rules here
    }
  }
}

I was having the same problem with a CakePHP 1.3 app that is using jQueryMobile to build mobile-friendly views. 我在使用jQueryMobile来构建适合移动设备的视图的CakePHP 1.3应用程序中遇到相同的问题。 I'll try to lay it out for future searches. 我会尝试将其布置以备将来搜索。

When I switched on $.mobile.ajaxEnabled = true; 当我打开$.mobile.ajaxEnabled = true; for jQM's nice Ajax-based navigation all the linked pages showed undefined instead of the page content. 对于jQM的基于Ajax的不错的导航,所有链接的页面都显示为undefined而不是页面内容。 The Ajax nav requires that the linked page have the proper structure, like so: Ajax导航要求链接页面具有正确的结构,如下所示:

<div data-role="page">
    <div data-role="header">
        <h1>Page Title</h1>
    </div><!-- /header -->
    <div data-role="content">   
        <p>Page content goes here.</p>      
    </div><!-- /content -->
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div><!-- /footer -->
</div><!-- /page -->

This markup was coming from two places for my app, though: my default mobile layout had the <html> , <body> and the page and footer divs. 不过,此标记来自我的应用程序的两个位置:我的默认移动布局具有<html><body>以及pagefooter div。 Each view had a header and content div. 每个视图都有headercontent div。

The problem arose because Cake's RequestHandler wanted to set the layout to ajax for all Ajax requests. 之所以出现此问题,是因为Cake的RequestHandler希望将所有Ajax请求的布局都设置为ajax It does this somewhere after beforeFilter() , which caused it to ignore a declared layout there (which Luke mentions). beforeFilter() 之后的某个地方执行此操作,这导致它忽略在那里声明的布局(Luke提到了)。

The ajax layout is empty, unlike my default layout--it doesn't have <html> and other markup because it assumes (rightly) that you only want partial markup that will be inserted into a page by Ajax. 与我的默认布局不同, ajax布局是空的-它没有<html>和其他标记,因为它(正确地)假设您只希望将部分标记由Ajax插入到页面中。 The jQuery Ajax-based navigation does want to see the full markup though, and when it didn't receive a well-formed page when making the Ajax request it had no page to display. 但是,基于jQuery Ajax的导航确实希望看到完整的标记,并且在发出Ajax请求时没有收到格式正确的页面时,就没有页面可以显示。

So the question became, "How do I make RequestHandler use my default mobile layout for mobile page requests?" 因此,问题变成了:“如何使RequestHandler对移动页面请求使用默认的移动布局?” I wasn't satisfied with Luke's solution above of disabling RequestHandler completely for mobile. 我对Luke上面的完全禁用Mobile的RequestHandler的解决方案不满意。 Luckily I found a post from May '11 with the same problem and a better solution: 幸运的是,我在11年5月发现了一个存在相同问题和更好解决方案的帖子:

Finally sorted it! 终于排序了! I was checking to see whether a mobile device was requesting the page using $this->RequestHandler->isMobile(), but had placed it in the beforeFilter() function. 我正在检查移动设备是否正在使用$ this-> RequestHandler-> isMobile()请求页面,但已将其放置在beforeFilter()函数中。 Moving this to the beforeRender() function in the app_controller.php file fixed the problem. 将其移至app_controller.php文件中的beforeRender()函数可解决此问题。 http://cakephp.1045679.n5.nabble.com/Jquery-Mobile-and-CakePHP-1-3-td4422871.html http://cakephp.1045679.n5.nabble.com/Jquery-Mobile-and-CakePHP-1-3-td4422871.html

So my code ended up something like this in AppController: 所以我的代码最终在AppController中变成了这样的样子:

function beforeRender() {
    if ($this->RequestHandler->isMobile()) {
        $this->layout = 'm_default';
    }
}

I hope that helps someone out there. 我希望这可以帮助某个人。

I was new to CakePHP and started with Version 2 several weeks ago. 我是CakePHP的新手,几周前开始使用Version 2。

So far I also keep the beforeFilter untouched to identifier isMobile() and finally use mobile-views within a Themed-Folder. 到目前为止,我还保持了beforeFilter不变,直到标识符isMobile()并最终在主题文件夹中使用移动视图。 Therefore I dont use subfolders for mobile-views within the default desktop-views. 因此,在默认的桌面视图中,我不对移动视图使用子文件夹。 After adding this->layout in the condition it seems that I got rid off the undefined which appeared only via some actions links. 在条件中添加this-> layout之后,似乎摆脱了仅通过一些action链接出现的undefined

public function beforeRender() {
    if($this->RequestHandler->isMobile()) {
        $this->theme = 'Mobile';
        $this->layout = 'default';
    }
}

The best sollution I think is to configure the request handler in before filter after the mobile browser was detected in your app controller: 我认为最好的解决方案是在应用控制器中检测到移动浏览器之后,在过滤器之前配置请求处理程序:

public function beforeRender() {
  if($this->RequestHandler->isMobile()) {
      $this->theme = 'Mobile';//set your theme
      $this->RequestHandler->ajaxLayout = 'public';//this does the trick, set your mobile layout, $this->layout is simply ignored or overwritten by the component
  }
}

Just set $this->layout = 'default'; 只需设置$this->layout = 'default'; in your controller, and it will use the default layout. 在您的控制器中,它将使用默认布局。

Or maybe you could make a header and footer element to put in your ajax and default layouts. 或者,您可以制作一个header和footer 元素以放入您的ajax和默认布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM