示例#1
0
 /**
  * Call
  *
  * Implements Slim middleware interface. This method is invoked and passed
  * an array of environemnt variables. This middleware inspects the environment
  * variables for the HTTP method override parameter; if found, this middleware
  * modifies the environment settings so downstream middleware and/or the Slim
  * application will treat the request with the desired HTTP method.
  *
  * @param   array $env
  * @return  array[status, header, body]
  */
 public function call(&$env)
 {
     if (isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'POST') {
         $req = new Slim_Http_Request($env);
         $method = $req->post($this->settings['key']);
         if ($method) {
             $env['slim.method_override.original_method'] = $env['REQUEST_METHOD'];
             $env['REQUEST_METHOD'] = strtoupper($method);
         }
     }
     return $this->app->call($env);
 }
 /**
  * Send HTTP response
  *
  * This method will set Response headers, set Response cookies,
  * and `echo` the Response body to the current output buffer.
  *
  * @return void
  */
 public function send()
 {
     if (!headers_sent()) {
         $this->sendHeaders();
     }
     if ($this->canHaveBody() && $this->request->isHead() === false) {
         echo $this->body;
     }
 }
示例#3
0
 /**
  * Get URL for named route
  * @param   string              $name   The name of the route
  * @param   array                       Associative array of URL parameter names and values
  * @throws  RuntimeException            If named route not found
  * @return  string                      The URL for the given route populated with the given parameters
  */
 public function urlFor($name, $params = array())
 {
     if (!$this->hasNamedRoute($name)) {
         throw new RuntimeException('Named route not found for name: ' . $name);
     }
     $pattern = $this->getNamedRoute($name)->getPattern();
     $search = $replace = array();
     foreach ($params as $key => $value) {
         $search[] = ':' . $key;
         $replace[] = $value;
     }
     $pattern = str_replace($search, $replace, $pattern);
     //Remove remnants of unpopulated, trailing optional pattern segments
     return preg_replace(array('@\\(\\/?:.+\\/??\\)\\??@', '@\\?|\\(|\\)@'), '', $this->request->getRootUri() . $pattern);
 }
示例#4
0
 /**
  * Call
  *
  * Implements Slim middleware interface. This method is invoked and passed
  * an array of environment variables. This middleware inspects the environment
  * variables for the HTTP method override parameter; if found, this middleware
  * modifies the environment settings so downstream middleware and/or the Slim
  * application will treat the request with the desired HTTP method.
  *
  * @param   array $env
  * @return  array[status, header, body]
  */
 public function call()
 {
     $env = $this->app->environment();
     if (isset($env['X_HTTP_METHOD_OVERRIDE'])) {
         // Header commonly used by Backbone.js and others
         $env['slim.method_override.original_method'] = $env['REQUEST_METHOD'];
         $env['REQUEST_METHOD'] = strtoupper($env['X_HTTP_METHOD_OVERRIDE']);
     } else {
         if (isset($env['REQUEST_METHOD']) && $env['REQUEST_METHOD'] === 'POST') {
             // HTML Form Override
             $req = new Slim_Http_Request($env);
             $method = $req->post($this->settings['key']);
             if ($method) {
                 $env['slim.method_override.original_method'] = $env['REQUEST_METHOD'];
                 $env['REQUEST_METHOD'] = strtoupper($method);
             }
         }
     }
     $this->next->call();
 }
 /**
  * Default Not Found handler
  * @return void
  */
 protected function defaultNotFound()
 {
     echo self::generateTemplateMarkup('404 Page Not Found', '<p>The page you are looking for could not be found. Check the address bar to ensure your URL is spelled correctly. If all else fails, you can visit our home page at the link below.</p><a href="' . $this->request->getRootUri() . '">Visit the Home Page</a>');
 }
示例#6
0
文件: Post.php 项目: stojg/puny
 /**
  * savePost saves / creates a post
  *
  * @param Slim_Http_Request $request
  * @param \stojg\puny\Post $post
  */
 public static function save_post(\Slim_Http_Request $request, Post $post)
 {
     $post->setContent($request->post('content'))->setTitle($request->post('title'))->setDate($request->post('date'))->setCategories($request->post('categories'))->setDraft($request->post('draft'))->save('posts');
 }
示例#7
0
 /**
  * Test get user agent string when not set
  */
 public function testGetUserAgentWhenNotExists()
 {
     $env = Slim_Environment::mock();
     unset($env['USER_AGENT']);
     $req = new Slim_Http_Request($env);
     $this->assertNull($req->getUserAgent());
 }
示例#8
0
 public function testDispatchWithoutCallable()
 {
     Slim_Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => '/hello/josh', 'QUERY_STRING' => 'one=1&two=2&three=3', 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim'));
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $res = new Slim_Http_Response();
     $router = new Slim_Router($req, $res);
     $route = new Slim_Route('/hello/:name', 'foo');
     $route->setRouter($router);
     $route->matches($req->getResourceUri());
     //<-- Extracts params from resource URI
     $this->assertFalse($route->dispatch());
 }
示例#9
0
 /**
  * Load session
  * @param   array $env
  * @return  void
  */
 protected function loadSession(&$env)
 {
     $req = new Slim_Http_Request($env);
     $value = Slim_Http_Util::decodeSecureCookie($req->cookies($this->settings['name']), $this->settings['secret'], $this->settings['cipher'], $this->settings['cipher_mode']);
     if ($value) {
         $_SESSION = unserialize($value);
     } else {
         $_SESSION = array();
     }
 }
示例#10
0
 /**
  * Test get refererer
  */
 public function testGetUserAgentWhenNotExists()
 {
     $env = Slim_Environment::getInstance();
     $req = new Slim_Http_Request($env);
     $this->assertNull($req->getUserAgent());
 }
示例#11
0
 public function testGetContentType()
 {
     //Case A
     $_SERVER['CONTENT_TYPE'] = 'application/json';
     $r1 = new Slim_Http_Request();
     $this->assertEquals($_SERVER['CONTENT_TYPE'], $r1->getContentType());
     //Case B
     unset($_SERVER['CONTENT_TYPE']);
     $r2 = new Slim_Http_Request();
     $this->assertEquals('application/x-www-form-urlencoded', $r2->getContentType());
     //Case C
     $_SERVER['CONTENT_TYPE'] = 'text/html; charset=ISO-8859-4';
     $r3 = new Slim_Http_Request();
     $this->assertEquals('text/html', $r3->getContentType());
 }
示例#12
0
文件: RequestTest.php 项目: ntdt/Slim
 public function testGetContentType() {
     //Case A
     $_SERVER['CONTENT_TYPE'] = 'application/json';
     $r = new Slim_Http_Request();
     $this->assertEquals($_SERVER['CONTENT_TYPE'], $r->getContentType());
     //Case B
     unset($_SERVER['CONTENT_TYPE']);
     $r = new Slim_Http_Request();
     $this->assertEquals('application/x-www-form-urlencoded', $r->getContentType());
 }