简体   繁体   English

在HTTP Request类中实现不同的请求方法

[英]Implementing different request methods in HTTP Request class

I'm currently writing my own HTTP Request class and I hit a small problem. 我目前正在编写自己的HTTP Request类,但遇到了一个小问题。 As you probably know, there are many request methods in PHP, like OPTIONS , GET , POST , PUT , HEAD , etc. but you cannot mix two or more of them in one request. 您可能知道,PHP中有许多请求方法,例如OPTIONSGETPOSTPUTHEAD等,但是您不能在一个请求中混合使用其中的两个或多个。 And this is where my problems begins. 这就是我的问题开始的地方。

I have HTTPRequest class, which has two static methods, that creates it's instance - create() , to create custom HTTP Request and createFromGlobals() , to create instance of current request recieved by server. 我有HttpRequest类,它有两个静态方法,创建它的实例- 创建(),创建自定义HTTP请求和createFromGlobals(),以创建由服务器收到当前请求的实例。

So, what's the problem? 所以有什么问题? The problem is, I want to find some way to separate request additional functions dependent on method (for example, I don't want to have getQueryString() function, when request method is PUT or POST) and I want the class to do it for me. 问题是,我想找到某种方法来分离依赖于方法的请求其他函数(例如,当请求方法为PUT或POST时,我不想拥有getQueryString()函数),并且我希望类来完成此工作为了我。 Also, I want to keep classes dependencies loose, so whole system will stay flexible. 另外,我想保持类的依赖关系松散,以便整个系统保持灵活。

Do you know any design pattern that resolves problem like mine, or any way to do this? 您是否知道可以解决诸如我的问题之类的任何设计模式,或采取任何方式来实现此目的?

Check out Symfony Components, it has a very nice Request class (among many other things), so you don't have to reinvent the wheel. 看看Symfony组件,它有一个非常不错的Request类(还有许多其他东西),因此您不必重新发明轮子。

http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Request.html http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/Request.html

I would probably implement what you want to achieve, similar to this (but keep in mind my comment about querystrings being valid with POST requests. At least, as far as I'm aware): 我可能会实现您想要实现的目标,类似于此(但请记住,我的关于查询字符串对POST请求有效的评论。至少据我所知):

abstract class HTTPRequest
{
    public static function createFromGlobals()
    {
        $method = $_SERVER[ 'REQUEST_METHOD' ];

        /* initialize additional arguments from globals */

        return self::create( $method /*, additional arguments */ );
    }

    public static function create()
    {
        $arguments = func_get_args();
        $method = array_shift( $arguments );
        $method = ucfirst( strtolower( $method ) );

        $className = __CLASS__ . $method;
        if( !class_exists( $className ) )
        {
            throw new Exception( 'Implementation for method ' . $method . ' not found' );
        }

        $reflector = new ReflectionClass( $className );

        return $reflector->newInstanceArgs( $arguments );
    }

    /* implement common functionality */
}

class HTTPRequestGet
    extends HTTPRequest
{
    /* implement additional GET functionality */
}

class HTTPRequestPost
    extends HTTPRequest
{
    /* implement additional POST functionality */
}

/* etc... */

Usage: 用法:

$requestFromGlobals = HTTPRequest::createFromGlobals();
$customRequest = HTTPRequest::create( 'post', array( 'additional', 'arguments' ) );

I was considering something like this: 我在考虑这样的事情:

<?php

class HTTPRequest
{
    protected $method;
    public $parameters
    // ...

    public static function create(/* ... */)
    {
        // ...
        switch ($this->method)
        {
            case 'GET':
                $this->parameters = new Parameters($_GET);
                break;
            case 'POST':
                $this->parameters = array(
                    'post' => new Parameters($_POST),
                    'files' => new Parameters($_FILES)
                );
                break;
            // ...
        }
        // ...
    }
}

What do you think about it? 你怎么看待这件事?

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

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