Intro to Composer // PHP Development Tutorial // Episode 9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello cyberspace my name is Garrett Mills and welcome to episode 9 in my PHP development tutorial series and in this episode we're going to take a look at composer composer is a dependency manager for PHP and is quite possibly the greatest development tool for PHP ever so we're going to take a look at setting that up and setting up some examples of composer in our project that we've got running in this video if you haven't I recommend starting at the beginning of this series so you get the foundation project and foundation concepts for PHP built up for this point which you can do by clicking the I on top right hand corner in this video but assuming you've done all that let's go ahead and get started so first off what is composer well according to its website composer is a dependency manager for PHP so what does that mean well a dependency in software development is a piece of software that someone else has written or that you've written in a different project that whatever you're working on right now it requires that piece of software to run it uses a bit of code written previously or written in a different project in the current project think of it as Legos if you're building a Lego house and so you've got your four walls built you have two options you can either spend a whole bunch of time building the roof piece by piece or you can use a pre-built roof that you built previously to build it on top of your existing walls that's essentially what a dependency is it's a bit of software it's that pre-built roof that we just reuse in different packages and different software so that we don't have to keep it inventing the wheel over and over again in every project and this allows us for much faster development this allows for more efficient development and it allows us as software developers to spend more time actually writing the program that we want to have functioning and less time writing all the background stuff that it requires to do the thing that we want it to do so a composer is a dependency manager for PHP so on a website called packages there are hundreds if not thousands of pre-built packages that have specific functionality for PHP and so what packages does is it provides an interface and it provides sort of a back-end for accessing these packages and using them in your software so what composer does is say you want to use this package this is a dot env package it reads a file and it stores the variables in that file as environment variables in PHP say you want to use this in your project well you tell composer that your project requires this package to function properly composer then when you install all of the packages for your project composer will look at this package it'll say ok I need to install the Jose Gonzales slash env package what does this package need well this package means m1 such a and B and PHP so composer is going to go and install m1 such and B and this package so it allows us to say okay I want this package and then composer behind the scenes handles all of the different software packages that your piece of kit your project requires to run so what we're going to do in this video is we're going to take a look at how to install composer and then we're going to set up a basic composer functionality in our project using this Jose Gonzales slash dot e MV package so the first thing that we need to do is install composer so if you're on a common Linux distribution so something that's based on Ubuntu for example you can just run the command from your software repositories to install composer so for example on Ubuntu I could run the command sudo app install composer and it'll go ahead and grab and install the composer packages for me now if you're on Windows there is a Windows installer file that I will have a link to in the video description that you can download and run on Windows and it'll set up the command line composer tool on your system for you and then if you're on Mac there is a little bit of script that composer has written for you that you will execute in your terminal so if you're on Mac you can go to this link the link to the composer download page which will be in the video description and you'll just one at a time run these four commands in your terminal so what this does is it uses PHP to download the composer installer verify that it downloaded properly and setup composer on your system so if you've done all that you should have composed or setup on your system so whether you're on Windows Mac or Linux the commands are all the same you can run composer and you should see an output that looks like this you should see composer at the top you should see the version and then some information about the commands that you can run so now that we've got composer installed we want to go ahead and open up our development environment so this is going to be the project that we've just sort of been building on since the beginning of this series so once we've got our development environment open what we need to do is create the composer JSON file now this composer JSON files sorted the central file the central information point for your project that you're building it tells composer what packages it needs to install so what what other pieces of code that your project needs and it tells it what versions it gives you some information about your project if you want it to and it's just the place where all the composer data is stored so what we need to do is we need to create the composer JSON file so we're going to create a new file name it composer got JSON and it's going to go in the main directory of our project now inside this file what we need to do is open a new set of curly braces and that's it save the file and close it the next thing that we want to do is we want to open up a terminal and what we want to do is change directories into the folder where we have our code so we can if it's stored in your Documents you want to do CD document slash PHP tutorial 1 or whatever so to do this we're gonna run the command composer require pausing in solids /v when we hit enter what it's gonna do is it's gonna find that composer JSON file and it's going to put a little bit of text in there telling it that our project needs this package and then it's going to go and find the reap the suggested version and it's going to install the package that we want and it's also going to install all the packages that this package needs to work so if we go line by line we can see that's exactly what it did it's going to use version 3.0 or higher of Jose Gonzales for /tmp and then it's going to install all of the packages that Jose Gonzalez lashed out and B needs to function properly so in this case it's M 1 /e and V and then it installed our package and then the command is finished so we can go ahead and close that and then if we look in our composer JSON file now we see we have this new entry called the require now require is the array where it stores the name and version of all of the packages that our project needs to function properly so we see we've got Jose Gonzalez SUV now the other new thing in our folder that you may notice is we have this vendor directory the vendor directory is where composer installs all of this software too so if we open up the vendor directory we see we have Jose Gonzales and if we look in here we have dot e and B and then inside this DMV is where all of the project files for Jose Gonzales / D and V live so the other and possibly most important file is inside the vendor direct and it's called autoload PHP this autoload PHP file is our entry point into all of the packages we've installed so inside this file is where composer is going to put the functions that it needs to set up all of the packages we install and so to access these packages all we have to do is tell PHP that we need to use anything that's inside this autoload PHP file so for example say we want to set up this dot env package in our index dot PHP file well up here above sessions start what we need to do is we need to tell it that we need to require everything inside that autoload file so we're gonna use the require so basically what require does is it tells PHP that it needs to import an entire other PHP file and we'll take a more in-depth look at require and it's sort of friends in the next video but we need to require a file so we get the current directory using two underscores dir followed by two underscores that gets the current directory and then we need to append a string telling it that our file is inside slash vendor slash autoload PHP so we need to require this autoload PHP file and what that's going to do is anytime we load this index dot PHP page it's going to go and run this auto load file and it's going to set up all of the packages we've installed with composer so now that we've loaded the composer auto load file we have access to all of the installed packages that we set up in our composer file so that means in this case the Jose Gonzalez / E and B so what we can do is we can just follow the instructions on that packages page for setting it up so what this package is going to do before we set it up is I just want to explain it what this package does is it gets from a file in our main directory and that file is called dot env and inside this file we can put variables and these variables are specific to our computer whatever computer we're running it what running the application on and the reason we want to put it in the dot MV file is we can refer to these variables from out throughout our code in many different places and if it changes at any point we can just change it in this dot env file and we don't have to change it in a hundred places in our code and also if you decide to install this app on a different computer with different settings you don't have to go through the actual code to change it you can just modify this dot env configuration file which saves you some time and it's a lot more secure so just for testing what we're gonna do is we're going to move this server address database name username and password we're gonna move these into our dot env file now ADAT env file doesn't use traditional PHP syntax it uses a different kind of syntax so we're gonna name this my SQL underscore address and then you don't need semicolons at the end and you want to use double quotes so Mike's well so what we're gonna do is we're going to set up this josé gonzález / DMV package to get the variables out of this DMV file and load them into PHP so that we can access them from our application so if we just follow the instructions on the packages page which is linked to in the description you can see we need to get a loader and so a loader means we need to get the loader object and the loader object is going to grab the dot env file so the loader object will talk about objects and object-oriented programming more in the next episode but basically this is just a special kind of class a PHP object that does a special job and we're loading it into this variable called loader so we see here when to put the path to our dot and be so we want to get the current directory using the two underscores dir variable and we need to append our dot e and E is just in this current directory so slash dot e and E so there we go that's going to give it our dot NV and then we're gonna parse it so this is some unfamiliar syntax but when you call a dash and then a greater than sign it just means that you're accessing a function that is in this item this object that we set here so if you remember we define these functions like handler well inside this loader object there are functions defined there's a function called parse and so we're just calling the parse function from this item and we'll talk about that more in the next episode and so once we've got all this code in here what it's going to do is it's going to go to find this talk in V file and it's going to load all of these variables into the PHP environment and the environments just a globally accessible variable so we can access it from anywhere in application so if we want to test this we can do a var dump of an environment variable now environment variables are set to the env superglobal so that's a dollar sign underscore env and this is an array and so we're gonna do a die after that so it stops at execution and then in here we can go ahead and get whatever variable we wants to see we want the my sequel address we can put that in there and now if we load up our web page we see we get the my sequel address as it was set in this DMV file and say we want to get to the database well we can do that too we just paste that in there and now we get the database so this allows us to set all of our variables in one place and reference them throughout our code so what we can do is here we can replace all of these with accessors for the environment variable and so this way we can reference all of these variables throughout our code and have them centrally set in one location where they're easy to change so obviously the actual DMV loader and the package that we set up wasn't exactly the point of this video the point was to get you familiar with using some of the basics of composer and so we looked at in this video setting up our composer JSON file where we tell composer what packages our application needs to run we set up a sample package and then we showed loading the auto load file which pulls in the packages that we installed through composer and it gives us access to them and we demonstrated how we have access to them by setting up our test package alright guys thanks for watching episode 9 my PHP development tutorial series as always will be down in the comments below if you have any questions you need help with anything if you want to show off what you're working on just connect with me down there you can also connect with me on Google+ on Twitter by email on my blog links do all that stuff are down in the description below along with links to all the resources I used in this episode also be sure to get subscribed so that you don't miss next week's video where we're going to take a look at getting started with object oriented programming and PHP as always guys thanks for watching this video and I'll see you in the next one [Music]
Info
Channel: Garrett Mills
Views: 2,868
Rating: undefined out of 5
Keywords: php, development, coding tutorial, glmdev, garrett mills, composer, dependency manager, glmdev.tech
Id: bxoUMJApUjs
Channel Id: undefined
Length: 16min 45sec (1005 seconds)
Published: Sun Feb 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.