/** * Get the form * * @param string $method http method to submit the form * @param string $action relative path from the web root. When set to null * it uses the current api method's path * @param bool $dataOnly if you want to render the form yourself use this * option * @param string $prefix used for adjusting the spacing in front of * form elements * @param string $indent used for adjusting indentation * * @return array|T * * @throws \Luracast\Restler\RestException */ public static function get($method = 'POST', $action = null, $dataOnly = false, $prefix = '', $indent = ' ') { if (!static::$style) { static::$style = FormStyles::$html; } try { /** @var Restler $restler */ $restler = Scope::get('Restler'); if (is_null($action)) { $action = $restler->url; } $info = $restler->url == $action && Util::getRequestMethod() == $method ? $restler->apiMethodInfo : Routes::find(trim($action, '/'), $method, $restler->getRequestedApiVersion(), static::$preFill || $restler->requestMethod == $method && $restler->url == $action ? $restler->getRequestData() : array()); } catch (RestException $e) { //echo $e->getErrorMessage(); $info = false; } if (!$info) { throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`'); } static::$info = $info; $m = $info->metadata; $r = static::fields($dataOnly); if ($method != 'GET' && $method != 'POST') { if (empty(Defaults::$httpMethodOverrideProperty)) { throw new RestException(500, 'Forms require `Defaults::\\$httpMethodOverrideProperty`' . "for supporting HTTP {$method}"); } if ($dataOnly) { $r[] = array('tag' => 'input', 'name' => Defaults::$httpMethodOverrideProperty, 'type' => 'hidden', 'value' => 'method'); } else { $r[] = T::input()->name(Defaults::$httpMethodOverrideProperty)->value($method)->type('hidden'); } $method = 'POST'; } if (session_id() != '') { $form_key = static::key($method, $action); if ($dataOnly) { $r[] = array('tag' => 'input', 'name' => static::FORM_KEY, 'type' => 'hidden', 'value' => 'hidden'); } else { $key = T::input()->name(static::FORM_KEY)->type('hidden')->value($form_key); $r[] = $key; } } $s = array('tag' => 'button', 'type' => 'submit', 'label' => Util::nestedValue($m, 'return', CommentParser::$embeddedDataName, 'label') ?: 'Submit'); if (!$dataOnly) { $s = Emmet::make(static::style('submit', $m), $s); } $r[] = $s; $t = array('action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'), 'method' => $method); if (static::$fileUpload) { static::$fileUpload = false; $t['enctype'] = 'multipart/form-data'; } if (!$dataOnly) { $t = Emmet::make(static::style('form', $m), $t); $t->prefix = $prefix; $t->indent = $indent; $t[] = $r; } else { $t['fields'] = $r; } return $t; }
/** * Find the api method to execute for the requested Url */ protected function route() { $this->dispatch('route'); $params = $this->getRequestData(); //backward compatibility for restler 2 and below if (!Defaults::$smartParameterParsing) { $params = $params + array(Defaults::$fullRequestDataName => $params); } $this->apiMethodInfo = $o = Routes::find($this->url, $this->requestMethod, $this->requestedApiVersion, $params); //set defaults based on api method comments if (isset($o->metadata)) { foreach (Defaults::$fromComments as $key => $defaultsKey) { if (array_key_exists($key, $o->metadata)) { $value = $o->metadata[$key]; Defaults::setProperty($defaultsKey, $value); } } } if (!isset($o->className)) { throw new RestException(404); } if (isset($this->apiVersionMap[$o->className])) { Scope::$classAliases[Util::getShortName($o->className)] = $this->apiVersionMap[$o->className][$this->requestedApiVersion]; } foreach ($this->authClasses as $auth) { if (isset($this->apiVersionMap[$auth])) { Scope::$classAliases[$auth] = $this->apiVersionMap[$auth][$this->requestedApiVersion]; } elseif (isset($this->apiVersionMap[Scope::$classAliases[$auth]])) { Scope::$classAliases[$auth] = $this->apiVersionMap[Scope::$classAliases[$auth]][$this->requestedApiVersion]; } } }
/** * Find the api method to execute for the requested Url */ protected function route() { $this->dispatch('route'); $params = $this->getRequestData(); $currentUrl = 'v' . $this->requestedApiVersion; if (!empty($this->url)) { $currentUrl .= '/' . $this->url; } $this->apiMethodInfo = $o = Routes::find($currentUrl, $this->requestMethod, $params); //set defaults based on api method comments if (isset($o->metadata)) { foreach (Defaults::$fromComments as $key => $defaultsKey) { if (array_key_exists($key, $o->metadata)) { $value = $o->metadata[$key]; Defaults::setProperty($defaultsKey, $value); } } } if (!isset($o->className)) { throw new RestException(404); } }