PHP for Beginners Part 2
CourseIn this second part of PHP for Beginners, you'll learn all about forms, form submissions, how this translates in the HTTP Request/Response world, and saving files to the disk. This series continues from PHP for Beginners Part 1, and we'll be expanding on that same project in this series.
In this tutorial we'll get things set up and ready to go by setting up our sample code, doing a quick review of the HTTP request, and creating a new page for the site with some navigation.
Additional resources
In this tutorial we're going to leave PHP alone for a second and put together some good old HTML to create a form with name, breed, weight, and bio text fields. Once we create our form, we'll explain how forms get submitted over HTTP, and the difference between POST and GET requests.
In this tutorial we will work with the PHP $_POST
variable, along with the $_SERVER
variable to capture the information from our form. We'll need to make sure our code is specifically looking for POST data, instead of GET, by using some logic.
We can now read the POST data from our form, but we can't really do much with it long term. We aren't saving the information anywhere. In this tutorial you are going to learn how to update our pets.json file with new information every time the form gets submitted. Since this is an important file for us to be able to use and read, we'll also quickly look at how to keep the human-readable formatting in place as we make our updates.
As you use your form, you'll notice that after you submit the form, you just keep seeing the form again. If you keep submitting it, you'll end up with a bunch of duplicate records. That's no good. In this tutorial you will implement a redirect on the form submission to take the submitter to the home page, instead of back to the form, using the header()
function. We'll explain what exactly a header is and how that relates to our HTTP requests.
Our form is working well now, and to finish up, in this tutorial we're going to clean up and reorganize our code a little bit by moving things into a new custom function, called save_pets()
. This won't change how the form works, but moving logic into functions gives us two advantages, re-usable code, and making it clearer what the code we've written does, and is intended for.
Additional resources
Drupal's Entity API
CourseDrupal Module Development
CourseWeek 1: Setup
CourseWeek 2: Create a New Theme
CourseWeek 3: Add an Asset Library
CourseThis tutorial is for PHP developers—including Drupal 7 developers—who want to get a local development environment up and running for Drupal 8 development work. In this tutorial, we will install Drupal 8, fire up the built-in PHP web server, set some variables in php.ini, initialize a Git repository, and discuss how Composer will impact what you commit to Git now and in the future. Finally, we'll walk through how to configure PhpStorm for Symfony development that plays nicely with Drupal 8 projects as well.
Additional resources
Development Environments
Local Development Guide (drupal.org)
PhpStorm
The Wonderful World of Composer — Drupalize.Me
PHP's Built-in web server (manual) — php.net
In this tutorial, we'll create a new module and create a route and controller for it. Remember hook_menu
? Well, hook_menu
is out and routes and controllers are in! If the YAML files in this lesson piqued your interest, check out our introduction to YAML tutorial to learn more.
Note: See Create an Info File for a Module for up-to-date instructions on info file requirements, which have changed since this video was recorded.
Additional resources
Create an Info File for a Module
An Introduction to YAML — Drupalize.Me
PHP Namespaces in 120 seconds
Clear Drupal's Cache
In this tutorial, we'll use an essential tool for Drupal 8 development: the Drupal Console. We'll take a look at how to list the commands available in Drupal Console and then use a Drupal Console command to clear the route cache.
Note: See the Drupal Console docs "Getting the project" page for the latest information on how to install Drupal Console. You will need to use Composer to install Drupal Console for each project.
Additional resources
Drupal Console — Download and Documentation
Drupal Console: Getting the Project
Tutorial: Drupal Console (Drupalize.Me)
Composer
In this tutorial, you will learn how to return a response through a controller using route wildcards. We'll walk through how the wildcard in the route was created and then how this wildcard gets passed as a parameter to the controller function, enabling you to display a value to the page based on the value in a particular URL path segment using Symfony's Response object.
We're assuming you have the Drupal Console up and running (which we set up in the previous tutorial). The drupal
command calls Drupal Console.
Curious about the inner workings of the HTTP response? Dive into Symfony and HTTP Fundamentals, which takes you from HTTP fundamentals to how PHP handles HTTP requests and responses, and integrates what Symfony's tools bring to this flow from request to response.
Additional resources
Symfony and HTTP Fundamentals — Symfony Documentation
Http Foundation Response Object — Symfony Documentation