It seems like you are using an ad blocker. To enhance your experience and support our website, please consider:

  1. Signing in to disable ads on our site. Our users don't see ads
  2. Or, Sign up if you don't have an account yet
  3. Or, disable your adblocker by doing this:
    • Click on the adblocker icon in your browser's toolbar.
    • Select "Pause on this site" or a similar option for lancecourse.com.

Workouts with Slim 3

By zooboole

What is Slim/Slim 3

Slim 3 or simply Slim is a PHP Micro-framework created by Josh Lockhart, Rob Allen, Gabriel Manricks, Andrew Smith with the help of its community. Version 3 is currently the highest version, which we're going to learn in this series. Its official documentation is here. And when I mention Slim I am making allusion to Slim 3.

Slim is a light-weight framework that can be used to develop websites and HTTP APIs of any type, and of any size. The term micro comes to show that light-weight aspect of it. Slim does three main things at its core from which any other things become possible:

  • First, Fast Routing -With the help of the tiny nikic/fast-route package. This means routing your HTTP requests with slim is going to be very easy and fun.

  • Secondly, HTTP messaging. Slim handles one of the best ways of sending/Receiving HTTP messages in accordance with the famous psr-7 rules.

  • And thirdly, a Dependency Injection Container which allows you to bring in more code(modules/packages) from other developers. It does this with the help of the great pimple/pimple Dependency Injection Container which is being backed up by container-interop.

Here you can see that Slim is about just three packages. In its earlier versions, Slim had just one package(I hope they will stick to the same philosophy). With this it seems to me you can learn the whole thing in few hours. So far, Slim has been installed by about 1,500,000 people worldwide.

A bit about HTTP messages and PSR-7

I made mention of how HTTP messaging works in this article recently.

The whole web is about sending requests to servers and receiving responses from them. This is like you talking to a teller in a bank and receiving his consigns. It's a communication -sending and receiving. Since this is done through the HTTP protocol, we talk of HTTP messaging. So HTTP Messages are just the Requests you send to the server through your browser or cURL client, and the Responses you get from the server after it has processed your request.

HTTP Messaging

Image by www.ntu.edu.sg

Since we browse websites over HTTP protocol through web browsers, it's important to understand how it works in order to prepare your prepare appropriately. With our applications we will define how the HTTP Request will be sent to the server and which type of HTTP response to receive. The PHP-FIG(PHP Framework Interop-Group) has put in place some interfaces representing all HTTP messages we can easily implement in our applications in order to make that easily. Slim 3 has therefore implemented PSR-7 interfaces.

Why Slim 3?

I have for so long had the passion for coding to the extend that I couldn't use CMSes to build websites and I didn't want to use any framework either until the arrival of Laravel which seduced me with the code aesthetic.

I love pure code. I love to understand and be conscious of what exactly I am doing. I want to be able to work on my code even after years. I want to be able to debug my code even when I am sleeping.

Many frameworks take this away from the developer because they come fully packaged. On its documentation page, Slim is defined as:

At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate callback routine, and returns an HTTP response. That’s it.

Yes, that's it. Slim is not meant to make you use unnecessary things. As I said it above, HTTP messages are the key of the web. Slim Handles that for you in the best way, the rest is up to you. Read more about Slim 3 features here. Even though you can do any thing with Slim 3, here are some advantages of using it:

  • Handling HTTP requests and Responses easily
  • Most advantages Laravel has, like Eloquent ORM, Migrations
  • Freedom to choose your own packages
  • Light and easy to get started
  • Easy and RESTFul Routing
  • Encourages good practice
  • Any package can be used
  • Use only needed packages
  • Flash messages
  • Caching
  • Templating
  • CSRF
  • Etc.

Conclusion

This is the introduction to this series on Slim 3. It's meant to help you see what Slim is, what you can possibility do with it, and why you should use it. In the next part we're going to see how we can install Slim 3 and how to set up our first application with it. Please stay around, the next part is soon. Share this with friends, it helps me a lot.

Last updated 2024-01-11 UTC