创建类型为post的CakeRequest实例-cakephp [英] Create instance of CakeRequest with type post - cakephp

查看:74
本文介绍了创建类型为post的CakeRequest实例-cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是同样的问题, 如何以编程方式创建CakeRequest实例?

Here is the same question, How to programmatically create instance of CakeRequest?

就我而言,我想创建post请求

just in my case I want to create post request

$request = new CakeRequest('/posts/view');
$request->data = array('key' => 'value');

我试图用这个

$request->addDetector('post', 
    array (
        'env' => 'REQUEST_METHOD',
        'value' => 'POST' 
    )
);

,但是当我在下一页回显$this->request->method()时,它总是输出GET.如何将请求类型设置为post?

but when I echo $this->request->method() on next page, it always outputs GET. How can I set request type as post ?

更新 蛋糕版本2.3

更新2

我实际上想做的是这样的:假设我有一个用于创建帖子的表格,其中包含标题和内容字段.现在,如果用户未登录,则在该表单下,我将显示登录/注册表单的输入(因此,该页面上只有一个表单),因此用户将填写用户名/密码以用于登录或注册信息,如果他选择注册,请单击signin and publishsignup and publish按钮.将帖子的信息保存在数据库表的临时行和/或会话中后,我需要对用户进行身份验证,为此,我要发送针对用户控制器的signup and publish操作的帖子请求,并且不更改那些注册/登录功能(正在检查发布请求),我想创建此发布请求.我知道我也可以将登录/注册动作移至某个组件,因此可以从不同的控制器使用它们,但是注册动作还有很多其他部分,并且还使用其他功能和组件,因此我不希望将所有这些移至其他组件逻辑融入组件.

what actually Im trying to do, is this: lets say I have a form for creating a post, with title and content fields. Now, if the user is not logged in, under that form I am showing signin/signup form's inputs(so I have just one form on that page), so the user will fill in the username/password for sign in or signup info and click signin and publish or signup and publish button, if he chose to signup. After saving the post's info in the db's table's temporary row and/or in session I need to authenticate the user, for that I want to send post request for users' controller's signup and publish action, and to not change the those signup/signin functions(were im checking the post request) I wanted to create this post request. I know I can also move signin/signup action to some component, so I can use them from different controllers, but signup action has a lot of other parts, and it uses other functions and components, and I would not prefer to move all that logic into component.

谢谢

推荐答案

您可以通过修改CakeRequest解析的PHP超全局变量来伪造发布请求:

You can fake a post request by modifying the PHP superglobals, which CakeRequest parses:

$_POST = array('key' => 'value');
$_SERVER['REQUEST_METHOD'] = 'POST';
$request = new CakeRequest('/posts/view');

但是这是黑客,应该避免.我无法想象您会想使用类似这样的情况.

However this is a hack and should be avoided. I can't imagine a scenario where you would want to use something like this.

这篇关于创建类型为post的CakeRequest实例-cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆