本文整理汇总了PHP中Request::getMethod方法的典型用法代码示例。如果您正苦于以下问题:PHP Request::getMethod方法的具体用法?PHP Request::getMethod怎么用?PHP Request::getMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Request
的用法示例。
在下文中一共展示了Request::getMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: run
public static function run(Request $request)
{
/** get the Controller, Method, Arguments/Parameters and the URL of the Controller */
$controller = $request->getController() . "Controller";
$method = $request->getMethod();
$args = $request->getArgs();
$controllerUrl = ROOT . "controllers" . DS . $controller . ".php";
if ($method == "index.php") {
$method = "index";
}
/** if the Controller exists, call the Controller */
if (is_readable($controllerUrl)) {
require_once $controllerUrl;
$fullController = "Controllers\\" . $controller;
$controller = new $fullController();
if (!isset($args)) {
$data = call_user_func(array($controller, $method));
} else {
$data = call_user_func_array(array($controller, $method), $args);
}
}
/** Render view */
$controllerUrl = ROOT . "views" . DS . $request->getController() . DS . $request->getMethod() . ".php";
if (is_readable($controllerUrl)) {
require_once $controllerUrl;
} else {
print "No se encontró la ruta especificada.";
}
}
示例2: testIsGetPostCli
/**
* The method of the request was set to get in the constructor
*
* @depends testConstructorNoParams
* @return null
*/
public function testIsGetPostCli()
{
$this->assertTrue($this->input->isGet());
$this->assertFalse($this->input->isPost());
$this->assertFalse($this->input->isCli());
$this->assertEquals('get', $this->input->getMethod());
$input = new AppInput('post');
$this->assertTrue($input->isPost());
$this->assertFalse($input->isGet());
$this->assertFalse($input->isCli());
$this->assertEquals('post', $input->getMethod());
$input = new AppInput('cli');
$this->assertTrue($input->isCli());
$this->assertFalse($input->isGet());
$this->assertFalse($input->isPost());
$this->assertEquals('cli', $input->getMethod());
/* prove not case sensitive */
$input = new AppInput('GET');
$this->assertTrue($input->isGet());
$this->assertFalse($input->isPost());
$this->assertFalse($input->isCli());
$this->assertEquals('get', $input->getMethod());
$input = new AppInput('POST');
$this->assertTrue($input->isPost());
$this->assertFalse($input->isGet());
$this->assertFalse($input->isCli());
$this->assertEquals('post', $input->getMethod());
$input = new AppInput('CLI');
$this->assertTrue($input->isCli());
$this->assertFalse($input->isGet());
$this->assertFalse($input->isPost());
$this->assertEquals('cli', $input->getMethod());
}
示例3: getResponse
public function getResponse()
{
try {
return new SuccesfullResponse($this->requestObject->getId(), $this->dispatchMethod($this->requestObject->getMethod(), $this->requestObject->getParams()));
} catch (\Exception $e) {
return new ErrorResponse($this->requestObject->getId(), array('message' => $e->getMessage(), 'code' => $e->getCode()));
}
}
示例4: invoke
/**
* Invokes controller's method of given route
*
* @param Route $route
* @return void
*/
public static function invoke(Route $route)
{
$ctrl_file = CTRLPATH . $route->controller . EXT;
$class_name = self::getClassNameByController($route->controller);
if (!$ctrl_file) {
App::notFound('Could not find controller "' . $route->controller . '" for route "' . $route->name . '"');
}
if (!is_readable($ctrl_file)) {
App::notFound('Cannot load file "' . $ctrl_file . '" for route "' . $route->name . '"');
}
if (!class_exists($class_name)) {
require $ctrl_file;
}
$ctrl_class = new $class_name();
$methods_to_try[] = 'action' . ucfirst(Request::getMethod()) . '__' . $route->action;
$methods_to_try[] = 'action__' . $route->action;
$called = false;
foreach ($methods_to_try as $method) {
if (method_exists($ctrl_class, $method)) {
call_user_func_array(array($ctrl_class, $method), array());
$called = true;
break;
}
}
if (!$called) {
App::notFound('Could not find valid method of class "' . $class_name . '" to execute');
}
unset($ctrl_class);
}
示例5: onRequest
/**
* Handles an incoming HTTP request and dispatches it to the appropriate action.
*
* @param Request $request The HTTP request message.
* @param Socket $socket The client socket connection.
*
* @return \Generator
*
* @resolve \Icicle\Http\Message\Response The appropriate HTTP response.
*/
public function onRequest(Request $request, Socket $socket) : \Generator
{
$dispatched = $this->app->getDispatcher()->dispatch($request->getMethod(), $request->getUri()->getPath());
switch ($dispatched[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// no route found
$randomStr = '';
for ($i = 0; $i < 1000; ++$i) {
$char = chr(mt_rand(32, 126));
if ($char !== '<') {
$randomStr .= $char;
}
}
$html = $this->app->getRenderer()->render('404', ['randomStr' => $randomStr]);
$sink = new MemorySink();
yield from $sink->end($html);
return new BasicResponse(404, ['Content-Type' => 'text/html', 'Content-Length' => $sink->getLength()], $sink);
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
// HTTP request method not allowed
$sink = new MemorySink();
yield from $sink->end('405 Method Not Allowed');
return new BasicResponse(405, ['Content-Type' => 'text/plain', 'Content-Length' => $sink->getLength()], $sink);
case FastRoute\Dispatcher::FOUND:
// route was found
$action = new $dispatched[1]($this->app);
$response = (yield from $action->handle($request, $dispatched[2]));
return $response;
default:
throw new \RuntimeException('Invalid router state');
}
}
示例6: handle
public function handle()
{
// Propagate the request based on the HTTPMethod of the request.
$method = \Request::getMethod();
switch ($method) {
case "PUT":
Auth::requirePermissions('tdt.input.create');
$uri = $this->getUri();
return $this->createJob($uri);
break;
case "GET":
Auth::requirePermissions('tdt.input.view');
return $this->getJob();
break;
case "DELETE":
Auth::requirePermissions('tdt.input.delete');
return $this->deleteJob();
break;
case "POST":
Auth::requirePermissions('tdt.input.edit');
return $this->editJob();
break;
default:
\App::abort(400, "The method {$method} is not supported by the jobs.");
break;
}
}
示例7: routeRequest
private function routeRequest(Request $request, Service $service)
{
$desired_method = String::underscoreToCamelCase($request->getMethod());
$requested_method = $request->getRequestMethod() . $desired_method;
$method = $requested_method;
$arguments = $request->getArguments();
if (!method_exists($service, $method)) {
$method = $request->getRequestMethod() . 'Router';
if (!method_exists($service, $method)) {
throw new MethodNotSupported($request->getService(), $requested_method);
}
array_splice($arguments, 0, 0, array($request->getMethod()));
}
$output = call_user_func_array(array($service, $method), $arguments);
$this->response->setResponse($output);
}
示例8: prepare
public function prepare(Request $request)
{
$headers = $this->headers;
if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
$this->setContent(null);
}
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getRequestFormat();
if (null !== $format && ($mimeType = $request->getMimeType($format))) {
$headers->set('Content-Type', $mimeType);
}
}
// Fix Content-Type
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset=' . $charset);
} elseif (0 === strpos($headers->get('Content-Type'), 'text/') && false === strpos($headers->get('Content-Type'), 'charset')) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
}
// Fix Content-Length
if ($headers->has('Transfer-Encoding')) {
$headers->remove('Content-Length');
}
if ('HEAD' === $request->getMethod()) {
// cf. RFC2616 14.13
$length = $headers->get('Content-Length');
$this->setContent(null);
if ($length) {
$headers->set('Content-Length', $length);
}
}
return $this;
}
示例9: run
public static function run($request)
{
if (!$request instanceof Request) {
$request = new Request($request);
}
$file = $request->getFile();
$class = $request->getClass();
$method = $request->getMethod();
$args = $request->getArgs();
$front = FrontController::getInstance();
$registry = $front->getRegistry();
$registry->oRequest = $request;
$front->setRegistry($registry);
if (file_exists($file)) {
require_once $file;
$rc = new ReflectionClass($class);
// if the controller exists and implements IController
// if($rc->implementsInterface('IController'))
if ($rc->isSubclassOf('BaseController')) {
try {
$controller = $rc->newInstance();
$classMethod = $rc->getMethod($method);
return $classMethod->invokeArgs($controller, $args);
} catch (ReflectionException $e) {
throw new MvcException($e->getMessage());
}
} else {
// throw new MvcException("Interface iController must be implemented");
throw new MvcException("abstract class BaseController must be extended");
}
} else {
throw new MvcException("Controller file not found");
}
}
示例10: matches
/**
* {@inheritdoc}
*/
public function matches(Request $request)
{
if (null !== $this->methods && !in_array(strtolower($request->getMethod()), $this->methods)) {
return false;
}
foreach ($this->attributes as $key => $pattern) {
if (!preg_match('#' . str_replace('#', '\\#', $pattern) . '#', $request->attributes->get($key))) {
return false;
}
}
if (null !== $this->path) {
if (null !== ($session = $request->getSession())) {
$path = strtr($this->path, array('{_locale}' => $session->getLocale(), '#' => '\\#'));
} else {
$path = str_replace('#', '\\#', $this->path);
}
if (!preg_match('#' . $path . '#', $request->getPathInfo())) {
return false;
}
}
if (null !== $this->host && !preg_match('#' . str_replace('#', '\\#', $this->host) . '#', $request->getHost())) {
return false;
}
if (null !== $this->ip && !$this->checkIp($request->getClientIp())) {
return false;
}
return true;
}
示例11: testConstruct
function testConstruct()
{
$request = new Request('GET', '/foo', ['User-Agent' => 'Evert']);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/foo', $request->getUrl());
$this->assertEquals(['User-Agent' => ['Evert']], $request->getHeaders());
}
示例12: send
/**
* Отправляет запрос.
*
* @param Request $request
* @return Response|NULL
* @throws \Exception
*/
public function send(Request $request)
{
$response = null;
$curlHandle = curl_init();
$requestMethod = $request->getMethod();
try {
switch (strtoupper($requestMethod)) {
case 'GET':
$response = $this->executeGet($curlHandle, $request);
break;
case 'POST':
$response = $this->executePost($curlHandle, $request);
break;
case 'PUT':
$response = $this->executePut($curlHandle, $request);
break;
case 'DELETE':
$response = $this->executeDelete($curlHandle, $request);
break;
default:
throw new \InvalidArgumentException("Current verb ({$requestMethod}) is an invalid REST verb.");
}
} catch (\Exception $e) {
curl_close($curlHandle);
throw $e;
}
curl_close($curlHandle);
return $response;
}
示例13: handle
public function handle($id = null)
{
// Delegate the request based on the used http method
$method = \Request::getMethod();
switch ($method) {
case "PUT":
return $this->put($id);
break;
case "GET":
return $this->get($id);
break;
case "POST":
if (!empty($id)) {
// Don't allow POSTS to specific configurations (only PUT and DELETE allowed)
\App::abort(405, "The HTTP method POST is not allowed on this resource. POST is only allowed on api/triples.");
}
return $this->post();
break;
case "PATCH":
return $this->patch();
break;
case "DELETE":
return $this->delete($id);
break;
case "HEAD":
return $this->head();
break;
default:
// Method not supported
\App::abort(405, "The HTTP method '{$method}' is not supported by this resource.");
break;
}
}
示例14: changePasswordAction
/**
* @Route("/change-password")
*/
public function changePasswordAction(Request $request)
{
$entityName = $this->container->getParameter('lemlabs_user.class');
// Prevent perfom logic below if user is authenticated, redirect to dashboard
if ($this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
return $this->redirect($redirectUrl);
}
$em = $this->getDoctrine()->getManager();
$salt = base64_decode($request->query->get('token'));
$user = $em->getRepository($entityName)->findOneBySalt($salt);
if (!$user) {
$this->get('session')->getFlashBag()->add('danger', 'Cannot found the user');
}
$form = $this->createForm(new PasswordType(), $user);
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
// Dont forget salt, common in security process
$user->setSalt(md5(time()));
$user->setPassword($encoder->encodePassword($user->getPassword(), $user->getSalt()));
$em->persist($user);
$em->flush();
$this->get('session')->getFlashBag()->add('success', 'Congratulations, your password has been updated correctly;');
return $this->redirect($this->generateUrl('lemlabs_login'));
}
}
return $this->render('LemLabsUserBundle:User:change-password.html.twig', array('form' => $form->createView(), 'errors' => $form->getErrors()));
}
示例15: handle
public function handle($uri)
{
$uri = ltrim($uri, '/');
// Delegate the request based on the used http method
$method = \Request::getMethod();
switch ($method) {
case "PUT":
return $this->put($uri);
break;
case "GET":
return $this->get($uri);
break;
case "POST":
case "PATCH":
return $this->patch($uri);
break;
case "DELETE":
return $this->delete($uri);
break;
case "HEAD":
return $this->head($uri);
break;
default:
// Method not supported
\App::abort(405, "The HTTP method '{$method}' is not supported by this resource ({$uri}).");
break;
}
}