Contents
Slim
A micro framework for APIs and small sites: routes, middleware and nothing extra.
1.Getting started
- Meet SlimWhat a micro-framework is, what Slim 4 includes, what it leaves out and which application this course builds.
- Installation and first runHow to install Slim 4 with Composer, create public/index.php and run the application on the PHP built-in server.
- The public folder and the web serverWhy every request goes to public/index.php, what the public folder is for and how to set up Apache and Nginx for Slim.
- RoutesHow Slim links an address and an HTTP method to code: methods, placeholders in the address, optional parts and constraints.
- Route names and groupsHow to name a route, build a link with urlFor and put routes that share an address prefix into a group.
- The requestThe PSR-7 request object in Slim: query parameters, form data and JSON, headers, the method and attributes.
- The responseThe PSR-7 response object in Slim: the body, the status code, headers, a JSON response and a redirect.
2.Application structure
- Project structureHow to lay out a Slim application in folders: config, src, public, class autoloading, a bootstrap file and a routes file.
- Action classesHow to move Slim route handlers out of functions into separate classes with an __invoke method and use them in the routes file.
- The dependency containerWhat a dependency container is, how to connect PHP-DI to Slim, how autowiring works and when definitions in config/container.php are needed.
- Settings and environmentWhere to keep Slim application settings: the .env file, the phpdotenv package, the array in config/settings.php and access through the container.
- MiddlewareWhat middleware is in Slim, how to write your own middleware class, attach it to all or to selected routes and in what order middleware runs.
- Built-in middlewareThree middleware that ship with Slim itself: body parsing, routing and error handling — and the right order to add them.
3.A site with a database
- Twig templatesHow to connect the Twig template engine to Slim with the slim/twig-view package, create a template in the templates folder and render it from an action.
- Layout and links in templatesHow to move shared page markup into a Twig layout, extend it, build links with url_for and pass shared data to every template.
- Connecting a databaseHow to connect a MySQL database to a Slim application: a posts table, connection values in .env and a PDO object in the container.
- The repository classHow to gather the queries for the posts table into a repository class with PDO prepared statements and use it in Slim actions.
- Forms and validationHow to show a form in Slim, receive its data in an action, validate it with your own class and print errors next to the fields.
- Sessions and flash messagesHow to start a PHP session in Slim with a middleware, keep data in it between requests and show one-time messages after a redirect.
- User loginHow to build email and password login in Slim: a users table, password verification, writing to the session and a middleware that closes routes to guests.
- CSRF protectionWhat CSRF request forgery is and how to protect the forms of a Slim application with the slim/csrf package: the Guard middleware and hidden token fields.
- File uploadsHow to receive a file from a form in Slim: the PSR-7 uploaded file object, checking the error, size and extension and saving to public/uploads.
4.API and going live
- Error handlingHow Slim turns exceptions into responses: HTTP exceptions with codes 404 and 403 and a custom error page through setDefaultErrorHandler.
- LoggingHow to connect the Monolog logger to Slim: the container definition, error logging by the error middleware and your own messages from actions.
- REST APIHow to build an API for posts in Slim: a resource route group, actions with JSON responses and correct status codes.
- Token access to the APIHow to close the modifying API routes in Slim: a tokens table, a middleware that checks the Authorization header and a short look at JWT.
- CORSWhy the browser blocks requests to an API from another site and how to allow them in Slim with a CORS header middleware and an OPTIONS route.
- TestingHow to test a Slim application with PHPUnit: the request is created in code, goes through the handle method, and the database is replaced in the container.
- DeploymentHow to put a Slim application on a production server: installing dependencies, the .env file, folder permissions, template and route cache, the update routine.