Helpers

Slim provides several helper methods (exposed via the Slim application instance) that will help you control the flowof your application.

Please be aware that the following application instance method helpers halt(), pass(), redirect() and stop()are implemented using Exceptions. Each will throw a \Slim\Exception\Stop or \Slim\Exception\Pass exception.Throwing the Exception in these cases is a simple way to stop user code from processing, have the framework take over,and immediately send the necessary response to the client. This behavior can be surprising if unexpected. Take a lookat the following code.

  1. <?php
  2. $app->get('/', function() use ($app, $obj) {
  3. try {
  4. $obj->thisMightThrowException();
  5. $app->redirect('/success');
  6. } catch(\Exception $e) {
  7. $app->flash('error', $e->getMessage());
  8. $app->redirect('/error');
  9. }
  10. });

If $obj->thisMightThrowException() does throw an Exception the code will run as expected. However, if no exceptionis thrown the call to $app->redirect() will throw a \Slim\Exception\Stop Exception that will be caught by theuser catch block rather than by the framework redirecting the browser to the “/error” page. Where possiblein your own application you should use typed Exceptions so your catch blocks are more targeted rather thanswallowing all Exceptions. In some situations the thisMightThrowException() might be an external component callthat you don’t control, in which case typing all exceptions thrown may not be feasible. For these instances we canadjust our code slightly by moving the success $app->redirect() after the try/catch block to fix the issues.Since processing will stop on the error redirect this code will now execute as expected.

  1. <?php
  2. $app->get('/', function() use ($app, $obj) {
  3. try {
  4. $obj->thisMightThrowException();
  5. } catch(Exception $e) {
  6. $app->flash('error', $e->getMessage());
  7. $app->redirect('/error');
  8. }
  9. $app->redirect('/success');
  10. });

Halt

The Slim application’s halt() method will immediately return an HTTP response with a given status code and body.This method accepts two arguments: the HTTP status code and an optional message. Slim will immediately halt the currentapplication and send an HTTP response to the client with the specified status and optional message (as the response body).This will override the existing \Slim\Http\Response object.

  1. <?php
  2. $app = new \Slim\Slim();
  3. //Send a default 500 error response
  4. $app->halt(500);
  5. //Or if you encounter a Balrog...
  6. $app->halt(403, 'You shall not pass!');
  7. //Or if you'd like to have a tea but you have no teapot at hand (and you respect [RFC7168](https://tools.ietf.org/html/rfc7168))
  8. $app->halt(418, 'Feel free to take me as teapot!');

If you would like to render a template with a list of error messages, you should use the Slim application’s render()method instead.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/foo', function () use ($app) {
  4. $errorData = array('error' => 'Permission Denied');
  5. $app->render('errorTemplate.php', $errorData, 403);
  6. });
  7. $app->run();

The halt() method may send any type of HTTP response to the client: informational, success, redirect, not found,client error, or server error.

Pass

A route can tell the Slim application to continue to the next matching route with the Slim application’s pass()method. When this method is invoked, the Slim application will immediately stop processing the current matching routeand invoke the next matching route. If no subsequent matching route is found, a 404 Not Found response is sent tothe client. Here is an example. Assume an HTTP request for “GET /hello/Frank”.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/hello/Frank', function () use ($app) {
  4. echo "You won't see this...";
  5. $app->pass();
  6. });
  7. $app->get('/hello/:name', function ($name) use ($app) {
  8. echo "But you will see this!";
  9. });
  10. $app->run();

Redirect

It is easy to redirect the client to another URL with the Slim application’s redirect() method. This method acceptstwo arguments: the first argument is the URL to which the client will redirect; the second optional argument is theHTTP status code. By default the redirect() method will send a 302 Temporary Redirect response.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/foo', function () use ($app) {
  4. $app->redirect('/bar');
  5. });
  6. $app->run();

Or if you wish to use a permanent redirect, you must specify the destination URL as the first parameter and theHTTP status code as the second parameter.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/old', function () use ($app) {
  4. $app->redirect('/new', 301);
  5. });
  6. $app->run();

This method will automatically set the Location: header. The HTTP redirect response will be sent to the HTTPclient immediately.

Stop

The Slim application’s stop() method will stop the Slim application and send the current HTTP response to theclient as is. No ifs, ands, or buts.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/foo', function () use ($app) {
  4. echo "You will see this...";
  5. $app->stop();
  6. echo "But not this";
  7. });
  8. $app->run();

URL For

The Slim applications’ urlFor() method lets you dynamically create URLs for a named route so that, were a routepattern to change, your URLs would update automatically without breaking your application. This example demonstrateshow to generate URLs for a named route.

  1. <?php
  2. $app = new \Slim\Slim();
  3. //Create a named route
  4. $app->get('/hello/:name', function ($name) use ($app) {
  5. echo "Hello $name";
  6. })->name('hello');
  7. //Generate a URL for the named route
  8. $url = $app->urlFor('hello', array('name' => 'Josh'));

In this example, $url is “/hello/Josh”. To use the urlFor() method, you must first assign a name to a route.Next, invoke the urlFor() method. The first argument is the name of the route, and the second argument is anassociative array used to replace the route’s URL parameters with actual values; the array’s keys must matchparameters in the route’s URI and the values will be used as substitutions.