In this video, Keith Casey walks through initial set up with Composer, the database, and loading the sample data. We also implement Hello World to make sure everything works
- [Instructor] Getting setup in Silex is a straight forward process. There are really only a few thing you need to do to go from an empty editor such as we have here to Hello World. Get rolling, we're gonna need out terminal, our editor, in this case, Sublime Text, and our browser, so let's do it. Since we're using Composer, this should be incredibly simple. We just use Composer's required function on the command line. So Composer require Silex/Silex, and we'll get a relatively recent version of 2.0.
And now, Composer, on our behalf, will download Silex and its dependencies and set them all up for us. This should be pretty fast, because Silex is a micro framework, but will depend on your internet speed. So now, we can go ahead and look at what we have here. In Composer, in the vendor folder are available and in our app, we now have those things visible. Now, like most modern frameworks, Silex uses a single entry point for the entire application. So before we dive in the code, let's make sure that Apache is set up correctly.
This has to be set up correctly or our API will not work. So let's make sure we get it right. We'll go ahead and create a new file. We'll call it .htxs. Usually your editor will warn you if you start a file with dot. In this case, we want to, that's the goal here. So we need to make sure that the Rewrite Engine is On. Then we need to make sure that the Rewrite Conditions are correct. So in this case, when Apache doesn't find a file that matches the one we're requesting, it'll go ahead and trigger this Rewrite Condition.
Let's copy it and apply it again. But this time, change the dash f to dash d. So we do the same thing for directories that Apache can't find. So now we want to decide what Apache will actually do with them. So in this case, we'll RewriteRule index.php. So now, what this does, is when Apache can't find a file or directory when we requested it, it'll take all the parameters, all the information, and pass it to index.php. This is exactly what we want. Let's start building our application.
Over here we'll create a new file. Call it index.php. Then we need to start filling in the details. We'll set it up as a php file. We'll require the vendor, auto load. We'll create a new app called Silex Application, and now we want to configure a default route. So the first route will always configure is Hello World. So in this case say Hello, we'll fill in a name. I'll explain that in just a moment. And we want to return, simple phrase, hello, app, escape, name.
And after all that is done, we want to activate the application. By app, run. So a couple of key things to call out here. You see the slash hello, slash name. The brackets around the name make this into a variable. It's a parameter on the URL itself. Then we use the function just to the right there to feed that variable directly into our application. So now, we go over to our browser. We should get this on the front page. But when we type Hello World, we should get Hello World.
So if you see that, you're ready to continue to the next step and start building your own API.
This course begins with a simple application specification and builds it one step at a time. Each chapter includes a key concept, with examples from other public APIs, and then shows how to build it yourself with Silex. Learn about URL routing, validating input, and generating response codes and hypermedia payloads. Like any project, the first implementation may be a little messy, but don't worry. The last chapter covers refactoring and what it takes to scale and support the API going forward.
- Understanding the project goals
- Adding authentication with Silex
- Using cross-framework and authentication middleware
- Creating a read-write API in Silex
- Uploading files via an API
- Adding file security
- Creating payloads and response codes in Silex
- Scaling your API