public function testRouteFullWithParams() { $this->app->get('/user/:id', function () { })->name('user'); $url = Url::route('user', array('id' => '1'), null, true); $this->assertEquals('http://apple.com/test/user/1', $url); }
/** * Get cookie * * @param string $key * @param mixed $default * @return mixed */ public function cookie($key = null, $default = null) { if ($key === null) { $_cookies = $this->injectors['_cookies']; $_option = $this->app->get('cookie'); if ($_option) { foreach ($_cookies as &$value) { if (!$value) { continue; } // Check crypt if (strpos($value, 'c:') === 0 && $this->app->has('cryptor')) { $value = $this->app->cryptor->decrypt(substr($value, 2)); } // Parse signed cookie if ($value && strpos($value, 's:') === 0 && $_option['secret']) { $_pos = strrpos($value, '.'); $_data = substr($value, 2, $_pos - 2); if (substr($value, $_pos + 1) === hash_hmac('sha1', $_data, $_option['secret'])) { $value = $_data; } else { $value = false; } } // Parse json cookie if ($value && strpos($value, 'j:') === 0) { $value = json_decode(substr($value, 2), true); } } } return $_cookies; } return isset($this->cookies[$key]) ? $this->cookies[$key] : $default; }
/** * Build current cookies * * @param callable $cb * @return $this|array|Output */ public function buildCookie(\Closure $cb = null) { if (!$this->injectors['cookies']) { return !$cb ? array() : $this; } if (!$cb) { $cookies = array(); } // Set cookie $_default = $this->app->get('cookie'); if (!$_default) { $_default = array('path' => '/', 'domain' => null, 'secure' => false, 'httponly' => false, 'timeout' => 0, 'sign' => false, 'secret' => '', 'encrypt' => false); } // Loop for set foreach ($this->injectors['cookies'] as $key => $value) { $_option = (array) $value[1] + $_default; $value = $value[0]; // Json object cookie if (is_array($value)) { $value = 'j:' . json_encode($value); } // Sign cookie if ($_option['sign'] && $_default['secret']) { $value = 's:' . $value . '.' . hash_hmac('sha1', $value, $_default['secret']); } // Encrypt if ($_option['encrypt']) { $value = 'c:' . $this->app->cryptor->encrypt($value); } $_option['maxage'] = $_option['timeout'] ? time() + $_option['timeout'] : $_option['timeout']; // Set cookie if ($cb) { $cb($key, $value, $_option); } else { $cookies[] = array('key' => $key, 'value' => $value, 'option' => $_option); } } if (!$cb) { return $cookies; } return $this; }