Custom

One route, multiple HTTP methods

Sometimes you may need a route to respond to multiple HTTP methods; sometimes you may need a route to respond to acustom HTTP method. You can accomplish both with the Route object’s via() method. This example demonstrates howto map a resource URI to a callback that responds to multiple HTTP methods.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->map('/foo/bar', function() {
  4. echo "I respond to multiple HTTP methods!";
  5. })->via('GET', 'POST');
  6. $app->run();

The route defined in this example will respond to both GET and POST requests for the resource identified by “/foo/bar”.Specify each appropriate HTTP method as a separate string argument to the Route object’s via() method. Like otherRoute methods (e.g. name() and conditions()), the via() method is chainable:

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->map('/foo/bar', function() {
  4. echo "Fancy, huh?";
  5. })->via('GET', 'POST')->name('foo');
  6. $app->run();

One route, custom http methods

The Route object’s via() method is not limited to just GET, POST, PUT, DELETE, and OPTIONS methods. You may alsospecify your own custom HTTP methods (e.g. if you were responding to WebDAV HTTP requests). You can define a routethat responds to a custom “FOO” HTTP method like this:

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->map('/hello', function() {
  4. echo "Hello";
  5. })->via('FOO');
  6. $app->run();