Development

Changeset 11254

You must first sign up to be able to contribute.

Changeset 11254

Show
Ignore:
Timestamp:
08/30/08 14:20:19 (5 years ago)
Author:
fabien
Message:

[1.2] added support for PUT and DELETE methods from a browser

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.2/UPGRADE_TO_1_2

    r11171 r11254  
    2727The remaining sections explain the main changes made in symfony 1.2. 
    2828 
    29 Configuration 
    30 ------------- 
    31  
    32 ### Request 
     29Request 
     30------- 
    3331 
    3432The `path_info_array`, `path_info_key`, and `relative_url_root` settings have 
     
    4240instead of being passed as an attribute. 
    4341 
    44 ### Response 
     42The request method constants from `sfRequest` values have changed from integers 
     43to strings and the `sfRequest::NONE` method has been removed: 
     44 
     45 **Constant** | **Old value** | **New value** 
     46 ------------ | ------------- | ------------- 
     47 GET          | 2             | GET 
     48 POST         | 4             | POST 
     49 PUT          | 5             | PUT 
     50 DELETE       | 6             | DELETE 
     51 HEAD         | 7             | HEAD 
     52 NONE         | 1             | - 
     53 
     54The `getMethod()` and `getMethodName()` methods now returns the same value, 
     55so `getMethodName()` is deprecated. 
     56 
     57The `sfAction::getMethodNames()` and the corresponding code in 
     58`sfValidationExecutionFilter` from `sfCompat10Plugin` have been removed. 
     59This method was deprecated in 1.1 and was not really useable in 1.0. 
     60 
     61You can now simulate `PUT` and `DELETE` requests from a browser by using the 
     62`POST` method and adding a special `sf_method` parameter: 
     63 
     64    [php] 
     65    <form action="#" method="POST"> 
     66      <input type="hidden" name="sf_method" value="PUT" /> 
     67 
     68      <!-- // ... --> 
     69    </form> 
     70 
     71Response 
     72-------- 
    4573 
    4674There is a new setting for the `response` factory: `send_http_headers`. 
     
    107135positions. In symfony 1.1, they take the position as a second argument. 
    108136 
    109 ### Prototype and Scriptaculous 
     137Prototype and Scriptaculous 
     138--------------------------- 
    110139 
    111140symfony continues to decouple its bundled software. In 1.2 the bundled Prototype and  
  • branches/1.2/lib/action/sfAction.class.php

    r10834 r11254  
    360360 
    361361  /** 
    362    * Retrieves the request methods on which this action will process validation and execution. 
    363    * 
    364    * @return int One of the following values: 
    365    * 
    366    * - sfRequest::GET 
    367    * - sfRequest::POST 
    368    * - sfRequest::PUT 
    369    * - sfRequest::DELETE 
    370    * - sfRequest::HEAD 
    371    * - sfRequest::NONE 
    372    * 
    373    * @see sfRequest 
    374    */ 
    375   public function getRequestMethods() 
    376   { 
    377     if (!sfConfig::get('sf_compat_10')) 
    378     { 
    379       throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.'); 
    380     } 
    381  
    382     return sfRequest::GET 
    383            | sfRequest::POST 
    384            | sfRequest::PUT 
    385            | sfRequest::DELETE 
    386            | sfRequest::HEAD 
    387            | sfRequest::NONE; 
    388   } 
    389  
    390   /** 
    391362   * Executes any post-validation error application logic. 
    392363   * 
  • branches/1.2/lib/plugins/sfCompat10Plugin/lib/filter/sfValidationExecutionFilter.class.php

    r9947 r11254  
    8383    } 
    8484 
    85     // get the request method 
    86     $method = $this->context->getRequest()->getMethod(); 
    87     if (($actionInstance->getRequestMethods() & $method) != $method) 
    88     { 
    89       // this action will skip validation/execution for this method 
    90       // get the default view 
    91       return $actionInstance->getDefaultView(); 
    92     } 
    93  
    9485    return $this->validateAction($filterChain, $actionInstance) ? $this->executeAction($actionInstance) : $this->handleErrorAction($actionInstance); 
    9586  } 
  • branches/1.2/lib/request/sfRequest.class.php

    r10627 r11254  
    2323abstract class sfRequest 
    2424{ 
    25   /** 
    26    * Process validation and execution for only GET requests. 
    27    * 
    28    */ 
    29   const GET = 2; 
    30  
    31   /** 
    32    * Skip validation and execution for any request method. 
    33    * 
    34    */ 
    35   const NONE = 1; 
    36  
    37   /** 
    38    * Process validation and execution for only POST requests. 
    39    * 
    40    */ 
    41   const POST = 4; 
    42  
    43   /** 
    44    * Process validation and execution for only PUT requests. 
    45    * 
    46    */ 
    47   const PUT = 5; 
    48  
    49   /** 
    50    * Process validation and execution for only DELETE requests. 
    51    * 
    52    */ 
    53   const DELETE = 6; 
    54  
    55   /** 
    56    * Process validation and execution for only HEAD requests. 
    57    * 
    58    */ 
    59   const HEAD = 7; 
     25  const GET    = 'GET'; 
     26  const POST   = 'POST'; 
     27  const PUT    = 'PUT'; 
     28  const DELETE = 'DELETE'; 
     29  const HEAD   = 'HEAD'; 
    6030 
    6131  protected 
     
    132102 
    133103  /** 
    134    * Retrieves this request's method. 
    135    * 
    136    * @return int One of the following constants: 
    137    *             - sfRequest::GET 
    138    *             - sfRequest::POST 
     104   * Gets the request method. 
     105   * 
     106   * @return string The request method 
    139107   */ 
    140108  public function getMethod() 
     
    146114   * Sets the request method. 
    147115   * 
    148    * @param int $methodCode  One of the following constants: 
    149    * 
    150    * - sfRequest::GET 
    151    * - sfRequest::POST 
    152    * - sfRequest::PUT 
    153    * - sfRequest::DELETE 
    154    * - sfRequest::HEAD 
     116   * @param string $method  The request method 
    155117   * 
    156118   * @throws <b>sfException</b> - If the specified request method is invalid 
    157119   */ 
    158   public function setMethod($methodCode) 
    159   { 
    160     $available_methods = array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD, self::NONE); 
    161     if (in_array($methodCode, $available_methods)) 
    162     { 
    163       $this->method = $methodCode; 
    164  
    165       return; 
    166     } 
    167  
    168     // invalid method type 
    169     throw new sfException(sprintf('Invalid request method: %s.', $methodCode)); 
     120  public function setMethod($method) 
     121  { 
     122    if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD))) 
     123    { 
     124      throw new sfException(sprintf('Invalid request method: %s.', $method)); 
     125    } 
     126 
     127    $this->method = strtoupper($method); 
    170128  } 
    171129 
  • branches/1.2/lib/request/sfWebRequest.class.php

    r11217 r11254  
    5050    parent::initialize($dispatcher, $parameters, $attributes, $options); 
    5151 
     52    // GET parameters 
     53    $this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET; 
     54    $this->parameterHolder->add($this->getParameters); 
     55 
     56    // POST parameters 
     57    $this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_POST) : $_POST; 
     58    $this->parameterHolder->add($this->postParameters); 
     59 
     60    $this->fixParameters(); 
     61 
    5262    if (isset($_SERVER['REQUEST_METHOD'])) 
    5363    { 
     
    5969 
    6070        case 'POST': 
    61           $this->setMethod(self::POST); 
     71          $this->setMethod(strtoupper($this->getParameter('sf_method', 'POST'))); 
    6272          break; 
    6373 
     
    102112    } 
    103113 
    104     // load parameters from GET/PATH_INFO/POST 
    105     $this->loadParameters(); 
     114    // additional parameters 
     115    $this->requestParameters = $this->parseRequestParameters(); 
     116    $this->parameterHolder->add($this->requestParameters); 
     117 
     118    if ($this->options['logging']) 
     119    { 
     120      $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))))); 
     121    } 
    106122  } 
    107123 
     
    279295  public function isMethod($method) 
    280296  { 
    281     $pathArray = $this->getPathInfoArray(); 
    282  
    283     return strtolower($method) == strtolower($this->getMethodName()); 
     297    return strtoupper($method) == $this->getMethod(); 
    284298  } 
    285299 
     
    291305  public function getMethodName() 
    292306  { 
    293     $pathArray = $this->getPathInfoArray(); 
    294  
    295     return isset($pathArray['REQUEST_METHOD']) ? $pathArray['REQUEST_METHOD'] : 'GET'; 
     307    if ($this->options['logging']) 
     308    { 
     309      $this->dispatcher->notify(new sfEvent($this, 'application.log', array('The "sfWebRequest::getMethodName()" method is deprecated, please use "getMethod()" instead.'))); 
     310    } 
     311 
     312    return $this->getMethod(); 
    296313  } 
    297314 
     
    772789  } 
    773790 
    774   /** 
    775    * Loads GET, PATH_INFO and POST data into the parameter list. 
    776    * 
    777    */ 
    778   protected function loadParameters() 
    779   { 
    780     // GET parameters 
    781     $this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET; 
    782     $this->parameterHolder->add($this->getParameters); 
    783  
    784     // POST parameters 
    785     $this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_POST) : $_POST; 
    786     $this->parameterHolder->add($this->postParameters); 
    787  
    788     // additional parameters 
    789     $this->requestParameters = $this->parseRequestParameters(); 
    790     $this->parameterHolder->add($this->requestParameters); 
    791  
     791  protected function fixParameters() 
     792  { 
    792793    // move symfony parameters to attributes (parameters prefixed with _sf_) 
    793794    foreach ($this->parameterHolder->getAll() as $key => $value) 
     
    799800      } 
    800801    } 
    801  
    802     if ($this->options['logging']) 
    803     { 
    804       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Request parameters %s', str_replace("\n", '', var_export($this->getParameterHolder()->getAll(), true)))))); 
    805     } 
    806802  } 
    807803} 
  • branches/1.2/test/unit/request/sfWebRequestTest.php

    r10060 r11254  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(34, new lime_output_color()); 
     13$t = new lime_test(37, new lime_output_color()); 
    1414 
    1515class myRequest extends sfWebRequest 
     
    143143$_SERVER['HTTP_X_FORWARDED_FOR'] = '10.0.0.1'; 
    144144$t->is($request->getForwardedRemoteAddress(), '10.0.0.1', '->getForwardedRemoteAddress() returns the value from HTTP_X_FORWARDED_FOR'); 
     145 
     146// methods 
     147$t->diag('methods'); 
     148$_SERVER['REQUEST_METHOD'] = 'POST'; 
     149$_POST['sf_method'] = 'PUT'; 
     150$request = new myRequest($dispatcher); 
     151$t->is($request->getMethod(), 'PUT', '->getMethod() returns the "sf_method" parameter value if it exists and if the method is POST'); 
     152 
     153$_SERVER['REQUEST_METHOD'] = 'GET'; 
     154$_POST['sf_method'] = 'PUT'; 
     155$request = new myRequest($dispatcher); 
     156$t->is($request->getMethod(), 'GET', '->getMethod() returns the "sf_method" parameter value if it exists and if the method is POST'); 
     157 
     158$_SERVER['REQUEST_METHOD'] = 'POST'; 
     159unset($_POST['sf_method']); 
     160$request = new myRequest($dispatcher); 
     161$t->is($request->getMethod(), 'POST', '->getMethod() returns the "sf_method" parameter value if it exists and if the method is POST');