Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
The models we just defined are the resources we'll be using for our API. Our endpoints will be specific locations where we will retrieve, create, and modify, and even delete these resources. Endpoints represent either a single record or a collection of records. We'll use the HTTP verbs to interact with these resources.
HTTP verbs mapped to CRUD operations
Create | :: | POST |
Read | :: | GET |
Update | :: | PUT |
Delete | :: | DELETE |
Trailing Slashes
Slim treats a URL pattern with a trailing slash as different to one without. That is, /user and /user/ are different and so can have different callbacks attached.
For GET requests a permanent redirect is fine, but for other request methods like POST or PUT the browser will send the second request with the GET method. To avoid this you simply need to remove the trailing slash and pass the manipulated url to the next middleware.
If you want to redirect/rewrite all URLs that end in a / to the non-trailing / equivalent, then you can add this middleware:
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$app->add(function (Request $request, Response $response, callable $next) {
$uri = $request->getUri();
$path = $uri->getPath();
if ($path != '/' && substr($path, -1) == '/') {
// permanently redirect paths with a trailing slash
// to their non-trailing counterpart
$uri = $uri->withPath(substr($path, 0, -1));
if($request->getMethod() == 'GET') {
return $response->withRedirect((string)$uri, 301);
}
else {
return $next($request->withUri($uri), $response);
}
}
return $next($request, $response);
});
Alternatively, consider oscarotero/psr7-middlewares’ TrailingSlash middleware which also allows you to force a trailing slash to be appended to all URLs:
use Psr7Middlewares\Middleware\TrailingSlash;
$app->add(new TrailingSlash(true)); // true adds the trailing slash (false removes it)
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up