From the course: Building APIs in PHP Using the Slim Micro Framework

From empty editor to Hello World

From the course: Building APIs in PHP Using the Slim Micro Framework

Start my 1-month free trial

From empty editor to Hello World

- [Instructor] Getting set up in Slim is a straightforward process. There's really only a few things you need to do to go from empty editor, such as we have here, to hello world. To get rolling, we're going to need our terminal, our editor, in this case Sublime Text, and our browser. Let's do it. First, we move to our terminal. Since we're using composer, we can simply do composer require slim, and specify version three point oh and above. Behind the scenes, composer will download Slim, and all of its dependencies, and set them up for us. This should be pretty fast, because Slim is considered a micro framework, but will depend on your Internet speed. Now, when we go back to Sublime Text, you can see, in our slim app folder here, we have a vendor folder with all of the dependencies. Next, like most modern frameworks, Slim uses a single point of entry for the entire application, so before we dive into the actual code, let's make sure that Apache is set up to serve our files properly. This has to be correct for our API, so let's make sure we get it right now. Within your project directory, create a new file, called HT access, well more properly, dot HT access. Most editors are going to warn you about using a dot, we want to do this on purpose. Within dot HT access, we go ahead and turn on the rewrite engine. That will make sure that when we pass it a URL, it translates it to the correct file. Now we have to write some rewrite conditions. These conditions will define what actually gets rewritten by Apache. In this case, when Apache gets a request for a file, that's what this dash F means, that it does not recognize, it will go ahead and pass it to another file, which we will get to in a second. We're going to copy and paste this, and do the same dash D, so that when we request a directory that doesn't exist, it will do the same thing. Now, when Apache gets a request for a file or directory that it doesn't recognize, it will pass it along to this. Every request will now go through index dot PHP. Depending on your Apache configuration, you might have to restart at this point, but quite often not. Now that we have this set up, we can go ahead and start writing PHP. Once again, we're going to go over to our Slim app folder, and create a file. In this case, we'll call it index dot PHP. It has to be named to that, because Apache will pass every file or directory it doesn't recognize to this file. We start this file it we normally would. We go ahead and open it with a PHP tag, and then we require our vendor autoload, so now all the libraries that we installed previously through Composer are now available to us. We say app, new, Slim. We have now instantiated our Slim application, and we can go ahead and add things to it. In this case, we'll start with a really simple path. We'll get into more detail on this later, and what this is doing. In the short term, let's go ahead and say hello name, and we'll pass a function in. I'm waving my hands a little bit through about this, but this will make more sense in just a moment. Now, whenever the Web server sees hello name is part of the path, it's going to go ahead and collect some parameters and pass them into this function here, so we need the request, the response, and the args. These are just the arguments that come in as part of the request. In here, we go ahead and we return the response. In this case, we're going to write hello, we're going to take the args, and pull out the name. This is where the bulk of the application work is actually done. The last step is we want to run the application. If all goes well, when we move over to our browser, we have Slim app, we say hello, monkey, it should say hello monkey. It's just as we expected. You have successfully set up Slim.

Contents