RequestHandler返回RSS

RequestHandler返回RSS

本文介绍了CakePHP RequestHandler返回RSS / JSON而不是HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到本周,我才开始愉快地移动自己的网站,直到我们开始发现一些错误,这些错误与我对Cake的RequestHandler的工作方式的误解有关,但是我还没有找到好的解决方法。

I've been moving merrily along with my site until this week when we've started to see some errors crop up that have turned out to be related to my misunderstanding the way Cake's RequestHandler works, but I have yet to find a good workaround.

我正在使用一种方法来查找和返回各种格式的内容,以便它们都存在于相同的URL上(也许这是我的错误,我需要将它们分开?)。因此,在我的控制器中,我有:

I am using one method to lookup and return content in various formats, so that they all live on the same URL (maybe this is my error and I need to split them up?). So, in my controller I have:

if($ this-> RequestHandler-> ext =='rss'){

获取RSS源。我还尝试使用 isRss()进行了更改,但发现这是问题的根源。我的问题是这样的:

to pick up on RSS feeds. I also tried using isRss() but changed it, figuring that was the source of my problem. My problem is this:

当从浏览器中访问此网址(我们称其为 / blog )时,我会通常会收到HTML响应。如果我在 /blog.rss 中找到它,则会收到RSS响应。到目前为止很好。但是,如果我将标头设置为接受 / blog 地址,接受:application / rss + xml; ,它将返回RSS,而不考虑没有文字扩展名.rss的事实。这些页面是缓存的,因此现在我的浏览器用户最终在请求 / blog 地址而不是HTML版本时看到RSS源的缓存版本。

When hitting this url (let's call it /blog) from a browser, I will normally get an HTML response. If I hit it at /blog.rss I get the RSS response. Great so far. But if I hit the /blog address with my header set as Accepts: application/rss+xml; it will return RSS regardless of the fact that there is no literal .rss extension. These pages are cached, so now my browser users end up seeing a cached version of the RSS feed when requesting the /blog address instead of the HTML version.

我检查了RequestHandler,结果发现它使用_setExtension()来基于Accepts标头设置 ext 值,即使不存在真实扩展名。似乎对此没有覆盖,因此似乎我不得不自己解析URL以查看是否确实存在扩展名,或者完全将这些URL分开,并且不允许rss / json与 / blog 地址空间(例如, / blog / feed / blog / json )。

I checked into RequestHandler and it turns out that it uses _setExtension() to set the ext value based on the Accepts header, even if no 'real' extension exists. There seems to be no override for this, so it looks like I'm either forced to parse the URL myself to see if there really is an extension or to separate out these URLs altogether and not allow rss/json from the /blog address space (eg, /blog/feed, /blog/json).

是否有更好/更轻松的方法来解决此问题,或者我对此设置方法所缺少的东西?我认为 ext 是确实存在扩展名的可靠指示,但也许还有另一种查找方式?

Is there a better/easier way to get around this or something that I'm missing with the way this is set up? I had figured ext was a reliable indication of there being an actual extension but maybe there is another way to look for it?

推荐答案

您部分正确。如果显式设置了没有扩展名,则会调用 _setExtension 函数,因此,像 / blog 将请求该功能,但如果您执行 /blog.rss ,则不会调用 _setExtension

You're partially right. The _setExtension function is called if there's no extension explicitely set, so a url like /blog will be requesting that function, but if you do /blog.rss, _setExtension won't be called.

无论如何,这只是为了阐明这一点。继续。

Anyway, that was just to make that point clear. Moving on.

阅读

所以,我在这里了解的内容是:如果将 html 传递给 Router :: parseExtensions ,则不会根据以下内容设置内容类型:接受的标题。为您的版本签出(第149行) ,如果您有 html 或<$,则有一个 if 会跳过扩展内容类型的覆盖c $ c> xhtml 首选类型。

So, what I understand here is: if you pass html to your Router::parseExtensions, then the content type won't be set according to the accepted headers. Checking out the code (line 149) for your version of cake, there´s an if that skips the content type override of the extension if you have html or xhtml on you preferred types.

if (count($similarTypes) === 1 && !in_array('xhtml', $preferredTypes) && !in_array('html', $preferredTypes)) {
         $this->ext = array_shift($similarTypes);
     }

尝试做

Router::parseExtensions('html', 'rss' /*and other extensions if you want*/)

看看是否能解决您的问题。

and see if that solves your problem.

这篇关于CakePHP RequestHandler返回RSS / JSON而不是HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:57