Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Introducing Dependency Management and Composer7:02 with Alena Holligan
The most effective developers are the ones who know what code to write and what code to use. These external "packages" of code are called dependencies, because your application depends on them. Composer is a tool to help you manage which packages, and which package versions, your application depends upon.
Composer - Dependency Manager for PHP
PHPMailer - full-featured email creation and transfer class for PHP
PHPUnit - The PHP Unit Testing framework.
composer require phpmailer 0
If composer require does not install the packages, make sure you run composer update. You can specify a single package to update.
composer require league/oauth2-google composer update league/oauth2-google
[MUSIC] 0:00 Hi Alena here, the most effective developers are the ones who know what 0:04 code to write and what code to use. 0:09 When you're able to use code that others have written, you're able to spend more 0:11 time writing code that you need to your own application. 0:16 These external packages of code are called dependencies, 0:20 because your application depends on them. 0:24 Composer is a tool, 0:27 that manages which version of these dependencies are required to be installed. 0:28 Composer installs those packages by downloading them into a folder 0:33 within your project. 0:38 You can then use composers autoload functionality 0:39 to include those dependencies on an as needed basis. 0:42 Before we get any further into how composer works. 0:47 Let's jump into work spaces and see how we can make composer work for us. 0:49 We could just download the code into our project and include it when we want. 0:54 But let's use composer to help a stay organized. 0:58 I'm going to set up, a simple application for sending HTML email. 1:04 The first thing I want to use, is the third party library or 1:07 package called phpmailer. 1:11 In the console, we type composer require and then enter. 1:15 Next, you'll be asked to search for a package. 1:23 In this case, we'll be using phpmailer. 1:26 Next, composer will list suggested packages. 1:31 Each package listed is prefixed with the vendor name. 1:34 Some vendors have multiple packages and this allows for 1:38 grouping and organizing those packages. 1:41 The first one is the one we want, phpmailer/phpmailer. 1:44 So we type zero and then enter again. 1:48 Composer then asks for the version constraint. 1:54 We'll go over versioning in more detail a little later. 1:56 For now, just leave it blank for the latest version and press enter. 1:59 We could add another package at this point. 2:05 We just press enter again to finish the setup. 2:07 Let's clear this screen and refresh our files. 2:10 We can now see composer.json. 2:18 We can then run composer install, And 2:21 composer will grab the required packages and place the files in a vendor folder. 2:27 Some dependencies have their own dependencies. 2:33 For example, phpunit for unit testing has many dependencies. 2:36 Making it a robust tool with less for the maintainers to worry about. 2:41 However phpmailer doesn't have any of its own dependencies, so 2:45 you won't see any here. 2:49 If we Refresh the files. 2:53 You will see an autoload file and a composer folder. 3:00 We'll jump back to autoloading in just a minute, 3:05 phpmailer has a suggestion for you. 3:08 If you want to use Google authentication, 3:12 you want to install the package league/auth2-google. 3:14 The vendor of this package league refers to The League of Extraordinary Packages. 3:19 This is a group of developers, who have banded together to build solid 3:25 well tested php packages using modern coding standards. 3:29 They have a wide range of packages for such things as Mongodb, 3:33 templating, JSON, markdown and NGO related tasks. 3:37 The league holds each other accountable for using standards and best practices, so 3:42 it's a good place to look for packages. 3:46 If we want to follow the suggestion we can simply type, composer require and 3:49 the full package, which is league/oauth2-google. 3:55 We can also add a version constraint as well, if we wanted to or just enter. 4:02 After using the require command, the added requirements should be installed. 4:13 If this doesn't happen or if you make additions or updates manually, 4:17 you'll need to run the composer update command. 4:21 If you don't want to update all your packages but only a specific package, 4:27 you can specify that package after update. 4:31 This time oauth2 requires some additional packages. 4:47 So composer installs those packages as well. 4:51 Great. 4:54 We can already see some of the benefits composer gives us. 4:55 We didn't have to download all those packages individually. 4:58 So, now how do we use those packages? 5:02 Let's close the console. 5:05 I told you that we get back to autoloading. 5:11 Autoloading, allows us to include a single file 5:15 which will then automatically load the classes when they're needed. 5:17 We use require the magic constant __DIR, 5:21 Then vendor, Autoload, .php. 5:30 Composer will then load the vendor files but only when needed. 5:40 Since we're only using phpmailer in our email file. 5:44 Composer will only load that package on our email page, and 5:47 the phpmailer classes referenced. 5:51 It will not load the phpmailer class on any of the other pages or 5:55 we might include the autoloader. 5:59 I've already included the code needed to make phpmailer work in work spaces. 6:01 So we can preview the site in a browser. 6:06 Choose new email and fill out the form. 6:09 You'll see that the new email now appears on the home page. 6:31 We can go back to the work spaces console and verify the email. 6:34 Type mail and you'll see the email we've just sent. 6:44 Type 1 and then we can see the details of the email. 6:49 Type exit to close the email. 6:54 And then we can close the console. 6:58
You need to sign up for Treehouse in order to download course files.Sign up