使用带有 url 扩展的 Cake
我正在使用 CakePHP 2.0(我相信它是 v2.0.3。)和 PHP 5.3.8。
我正在开发一个利用 Cake 对 url 扩展的支持的应用程序。具体来说,只要 url 请求以 .xml 扩展名结束,我就会输出 XML。如果发出的 url 请求没有任何扩展名,那么我的应用程序将显示标准视图。这一切都工作得很漂亮——
request .../controller/action.xml renders via view/controller/xml/action.ctp while request .../controller/action renders via view/controller/action.ctp.
为了实现这一点,我做了以下工作:
1. Added support for url extension; added the following line to route.php -- Router::parseExtensions('xml');
2. Added support request handling; added the following line to MyController.php -- public $components = array('Session', 'RequestHandler');
为了输出 xml,我将 Cake 的“XML”类与 PHP 的“SimpleXMLElement”类结合使用。我的问题是永远不会生成完整的 xml。这些类应该根据输入的 PHP 数组生成 xml,但是,似乎未处理完整的数组。我的 xml 输出是部分的。
我的视图文件 (.ctp) 中的源代码如下:
$simple_xml_elem = Xml::build($xml_array); echo $simple_xml_elem->asXML();
有趣的是,在尝试调试这个问题的过程中,我发现如果我简单地尝试在 xml 视图文件 (../view/controller/xml/action.ctp) 中转储视图对象,可以观察到类似的行为。 'var_dump($this)' 仅输出视图的部分转储。在标准视图文件 (../view/controller/action.ctp) 中执行的相同视图转储会输出视图的完整转储。
我相信,Cake 在路由 url 扩展时与请求标准视图时以某种方式设置不同的视图环境。
在我失去头发之前,请有人帮我解释一下。请????谢谢!
I am using CakePHP 2.0 (I believe it is v2.0.3.) and PHP 5.3.8.
I am working on an application which utilizes Cake's support for url extensions. Specifically, I am outputing XML whenever a url request ends in the .xml extension. If the url request is made without any extension, then my application presents the stardard view. This all works beautifully --
request .../controller/action.xml renders via view/controller/xml/action.ctp while request .../controller/action renders via view/controller/action.ctp.
To achieve this, I did the following:
1. Added support for url extension; added the following line to route.php -- Router::parseExtensions('xml');
2. Added support request handling; added the following line to MyController.php -- public $components = array('Session', 'RequestHandler');
To output xml, I am using Cake's 'XML' class in conjunction with PHP's 'SimpleXMLElement' class. My problem is that the complete xml is never generated. The classes are suppose to generate xml based on an input PHP array, however, it appears that the complete array is not processed. My xml output is partial.
My source code in my view file (.ctp) is as follows:
$simple_xml_elem = Xml::build($xml_array);
echo $simple_xml_elem->asXML();
Interestingly, in the course of trying to debug this problem, I discovered the a similar behaviour can be observed if I simple attempt to dump the view object within the xml view file (../view/controller/xml/action.ctp). 'var_dump($this)' only output a partial dump of the view. The same view dump performed within the standard view file (../view/controller/action.ctp) outputs a full dump of the view.
It is my believe that Cake is somehow setting up the view environment differently when it routes for a url extension than when the standard view is requested.
Could some please shed some light on the for me before I lose my hair. Please???? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看过回复了吗?请求对象
http://mark-story.com /posts/view/the-cakerequest-object-in-cakephp-2-0 - 这篇文章是在 cake 2 是 alpha 版本时写的,我认为但 Mark 谈到了请求对象的概念(响应的另一个条目
)对象被传递到控制器
http://book.cakephp.org/2.0/en/controllers/request -response.html
现在我相信您使用请求 comp 来处理布局切换(尽管没有它也可以完成)
Have you looked at the response & request objects
http://mark-story.com/posts/view/the-cakerequest-object-in-cakephp-2-0 - the article was written when cake 2 was alpha I think but Mark talks about the concept of request object (another entry for response)
both objects are passed to the controller
http://book.cakephp.org/2.0/en/controllers/request-response.html
for now I believe you use the request comp to handle layout switching (although it can be done without it)