The building blocks of creating a simple POST/GET REST API using Symfony 3. Though not production ready, this is a beginners guide to routes, controllers, and the request response ideology.
very simple RESTful Web API during this talk. Follow along and/or checkout https://andrewadcock.com/a-simple-restful-api-tutori al-with-symfony-3/ for more information. Our API will allow for adding (POST) and retrieving (GET) content from a database.
a web ready REST API in 8 minutes? Yes! But don’t. That’s not the point of this presentation. We are going to create a very simple (and insecure) API, and focus on the core principles of creating an API with Symfony 3.
of rules, functions and tools used by developers to interact with, or create, software. The API describes what functionality is available and in what way you can utilize that functionality.
will help symfony understand how to handle your code. When using the PhpStorm plugin, this will help more with auto-hinting. app_api: resource: ‘@AppBundle/Controller/Api/v1/ type: annotation
the file /src/AppBundle/Controller/Api/v1/QuoteController.php Why is /v1/ in there? THE FUTURE! We will likely upgrade our API in the future. We are preemptively setting up for version 2, 3, 4, etc of our API
our controller will extend the Symfony FrameworkBundle Controller. This will give our QuoteController access helper functions and the service container. Learn More: http://symfony.com/doc/current/controller.html
help you with your code. Xdebug, PSR support, and lot’s of plugins. The Symfony plugin, by Daniel Espendiller, offers lot’s of code hints for all aspects of Symfony development! Learn more: https://plugins.jetbrains.com/plugin/7219-symfony-plugin
via the POST method. In our API we will create a method named newAction() that will extract data from the url’s query string. Example: http://127.0.0.1/api/v1/quote?actor=Farnsworth"e=Yes%20if%20b y%20allow%20you%mean%20force
provide more information about our code. Let’s add a Route and Method to our PHP DocBlock. @Route(“/api/v1/quote”) @Method(“POST”) (Reminder: Don’t forget to add the appropriate use statements)
a parameter. newAction(Request $request){} Adding this parameter lets us accept, and use, the Request coming into the server. After processing that data we can send a Response.
get individual values $value = $request->query->get(‘field’) The Request holds query information, server info, cookies and more. Try var_dump($request) to see everything in the Request object.
create a database, some tables and users. This is fine but a little time consuming. Symfony has made this much easier and faster for us by utilizing a beautiful tool: Doctrine!
are tools that we can use to interface with our database. “The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping.” - http://octrineproject.org
row in the database, we need to send a response. For the simplicity of this talk, we will just return a string and an HTTP response code. We will respond with 201 created: The request has been fulfilled, resulting in the creation of a new resource.
method to add it to the database, now, it’s time to retrieve that data so that we can use it. Create a new method called getAction() and require an id.
and works in a closed environment. Next we need to authenticate users, verify data sources, and tighten up security. Review these slides and you’ll see that a basic Symfony RESTful API can be created in 8 minutes.