To wrap things up on this project, we want to update the contact page with the same HTML layout as we have on the home page. To do this, we'll move the HTML sections into their own PHP files, and use require
again to get them into the right places.
Sharpen your object-oriented skills by exploring the ideas and reasons behind dependency injection. This simple principle separates developers who write functional code from those that are able to build great, and maintanable applications. In this series, we'll see dependency injection in action, why it's important, and how it relates to services and service-oriented architecture. We'll also refactor our demo application to use a dependency injection container, using a fantastic—but simple—container called Pimple.
In this tutorial, we’ll be coding with a real example where we create a simple app to help people give their money away, we’re calling it SendMoneyToStrangers.com. We’ve already bootstrapped a small app, which you can download and use if you want to follow along. It uses an SQLite database, so if you don't have that set up, you can grab the database and find installation instructions on the SQLite site. Once you SQLite, you will install the app from the demo code we are providing. We will also be using Composer to make it easier to get Pimple when we need it. If you are not familiar with Composer, you can watch the short tutorial The Wonderful World of Composer to get up and running.
Additional resources
SQLite site
The Wonderful World of Composer tutorial
Pimple
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
Our app is small now, but as it grows, the app.php file will get harder and harder to read. The best way to fix this is to separate each different chunk of functionality into different PHP classes and methods. Each of these classes is called a “service” and the whole idea is sometimes called Service-Oriented Architecture. In this tutorial you're going to create a new service, and learn about different ways to access external objects from a service. Then, we'll set up our app to use dependency injection to get the job done right.
Additional resources
Service-Oriented Architecture
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
We’ve already created our first service and used dependency injection, but one problem with the FriendHarvester is that we’ve hardcoded the SMTP configuration inside of it. What if we wanted to re-use this class with a different configuration? Or what if our beta and production setups use different SMTP servers? Right now, both are impossible! In this tutorial we're going to see how we can also inject configuration or an entire service. In the end we're going to inject the entire SmtpMailer object and build an interface for it. Through this process we'll make our code more flexible, since it will be able to accept any object that implements MailerInterface.
Additional resources
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
Our project now has services, an interface, and is fully using dependency injection. Nice work! One of the downsides of DI is that all the complexity of creating and configuring objects is now your job. This isn’t so bad since it all happens in one place and gives you so much control, but it is something we can improve! If you want to make this easier, the tool you need is called a dependency injection container. A lot of DI containers exist in PHP, but we're going to use Composer to grab the simplest one of all, called Pimple. (If you are unfamiliar with Composer, you should watch The Wonderful World of Composer tutorial.)
Additional resources
The Wonderful World of Composer
Composer download
Pimple
To learn how to apply these concepts in Drupal 8 module development, check out the Module Development Essentials series, starting with Understand the Service Container.
Welcome to the wonderful world of Composer, the Dependency Manager for PHP! In this tutorial, you'll learn how to install and configure Composer and use it to integrate third-party libraries into any PHP project. We'll walk through:
- How Composer makes sharing awesome again
- Downloading the composer.phar file
- Creating the composer.json file
- Installing the external libraries
- Handling autoloading
- Understanding the composer.lock file
- The update versus install command
- The require command
- Storing in version control
Additional resources
- Composer website
- Packagist
- http://bit.ly/composer-dump-autoload
- http://bit.ly/composer-require-dev
- http://bit.ly/composer-min-stability
- http://bit.ly/composer-global-install
Also, check out our topic page on Composer for even more resources.
Now it's time for you to use dependency injection containers in your own projects. In this tutorial we talk briefly about some other containers out there you might want to check out.
Still not quite comfortable with PHP 5.3 namespaces? No problem! Give us 120 seconds and we'll introduce you to all the crazy characters ("namespace", "use" and "\") and show you how they work.
Additional resources
PHP Namespaces in under 5 Minutes (symfonycasts.com)
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
Now that we've created a service, let's configure it. In this tutorial, you'll learn how to register your new service. We'll also be using Drupal Console to ensure everything is working as it should.
Note: The Drupal Console command container:debug
is now debug:container
.
Note: Drupal Console is no longer maintained. There are other tools and methods for getting a list of services in the container. See the tutorial Discover and Use Existing Services to learn more.
Additional resources
Discover and Use Existing Services
Dependency Injection and the Art of Services and Containers — Drupalize.Me
An Introduction to YAML — Drupalize.Me
In this lesson, you will learn how to get a Symfony 3 project started. First download Symfony and then follow along with this video tutorial.