Composer Tutorial Part 1 – How to use Composer



hi I'm Raval and today we're going to be talking about composer so what is composure composure is a dependency manager for PHP it enables us to download packages from the web straight from our terminal it also has its own auto loader to enable Auto loading I won't be covering that in today's lesson but look out for a future lesson on Auto loading and composer why do we need composer compose ensures that all developers who are working together on a project run the correct versions of the correct packages that are needed for the project composer also makes it very easy for any developer to just get up and running with the project really quickly and download the correct packages it also enables us to start with also loading very quickly again we won't be covering also loading here but it's good to know so getting started what you need to do is head over to gap composer dog where you can download and install composer from for Windows users you can just download the setup file run it and it's quite self-explanatory for mac and linux users i'm a bit unsure just take a look at your compositor dog there's a bunch of information there installation instructions for you so when you're installing composer it'll give you the option of whether you want to install it to your project or you want to make it available globally and I suggest make sure you make it available globally the reason being is that composing is pretty much an integral part of modern PHP and modern PHP applications it's advisable that you use it for your own applications so keeping it around is just very good so after this you're pretty much ready to require your first package but before you do require your first package what I want you to understand is the difference between a composer to JSON and a composers lockfile these are the two files their control dependencies it composes a JSON and a composer that lock you can first create a composite of JSON file and a composer that json file will declare the composer the vendor name of the package you would like to install and the version number and you compose it a JSON file you don't need to be explicit about what version number you will be downloading or composer will be downloading you can save version one point X point X or one point to point X you can pretty much give composer the option of deciding what what is the latest version of that package and have composer downloaded for you so that can be a handy feature a composer door lock file will be created after you install the first dependency so you can either create a composer to JSON manually or you can have composer do it for you but once composer grabs these packages and installs it it creates a composer lock file and the purpose of the composer log file is to declare to composer that this is the exact package I downloaded just like compositor JSON but the difference is it composed that lock will explicitly say what version was installed whereas in composer to json can be it can be variable so like I said point for composer to JSON can point to the latest versions composer lock will point to the version that's currently installed so composer install so composer install will be the first command that I introduce to you guys and what it does is is it as it says it it stalls the packages so compose install will look for a composer to JSON file it will then look to see anything is declared and it will try to install these dependence these first a composers install will check if a composer that lock file exists if no composer log file exists composer update will run which will automatically create the composer lock file if a composer lock file already exists then the declared packages will be installed so let me explain that a little bit further so like I said before you can explicitly or not explicitly declare packages in your composer that JSON file when you run composer it'll go to this JSON file it'll say okay I need to I need to install these packages but first what it does is it checks to see well do I have a lock file that explicitly states this is what's already installed and if it does have a lock file it's going to ignore the JSON and install what the lock file has but there's no lock file it's going to go to the JSON file install those packages and then create a log file for you I hope that sinks in and you know if that doesn't sink in re-watch what I just said a couple times I hope you really understand it if you still a little bit confused just sound off in the comments below I'll be happy to answer any questions composer update so composer update is the next command I'm going to introduce you to and what composer update does is it checks the composite of JSON file so like I said a composite of JSON file can have variable versions so what composer will do is will determine the latest version to install based on once declared and your composer to JSON file it'll then go and install the latest versions regardless if the composite of lock file says you know this is already installed at version 1.2 0.3 if version 1.2 point 4 is available composer update will grab one point two point four install it and finally update the composer lockfile to reflect these changes once again this is very important and I really hope it sinks in you really need to understand the difference between a composer to JSON file composer log file and the difference between composer install and composer update so let's go and try and get our first package bring up sublime and bring up I'll get bash here so notice we have a folder here we can get rid of that one let's keep Dinah one so we can see we have a image of Dino just make that a bit bigger t-rex we have indexed a PHP file which is blank for now so I want to grab a package that will help me resize that Dino dot JPEG and I know of one called intervention image I can either create my package JSON file for composer to read or I can have composer you know created for me and I'm let's say I'm not going to do that so I'm going to go over to package to packages and I'm going to search for intervention image there we go great so there's a great page here that says explains a whole bunch of things about project there's a link to the github page and to the intervention image website but all you need to focus on is this line here composer require intervention image I'm going to copy that go back to bash paste that in forgot the C and just to let you know here it's composer require it's because it's going to create my JSON file for me let's run that okay so that's running now it's grabbing a version of intervention image for me and you can see it's created a composer lock composite a JSON and a vendor folder as well so let's take a look at is composer dot JSON says intervention image 2.3 composure lock has a bunch of things in it if we look down for image intervention or intervention image it actually installed version 2.3.6 okay and in our vendor folder we have intervention here so I want to use intervention I already have some code here I'm just going to paste that there and save that so what's happening here is it's setting up the auto load again please don't pay attention to that right now focus on this bit here so this and PHP just show you intervention image has an example here and more examples on their website so you can just take that and what it's going to do is going to take my dyno image resize it and save it with a hundred percent quality and then I'm just going to echo it out to the page so let's see if that worked refresh and yes it has is created I know too so we have a large dynamic there and a much smaller one down there so that's installing dependencies with composer okay guys so I really hope that that has made a whole bunch of things clearer for you here are some links to composer itself and as well as packages if you have any questions I know it's if you know if you new to this it can be a lot to understand just sound off the comments below I'm more than happy to answer any questions and please please remember to like and subscribe thanks for watching guys

18 thoughts on “Composer Tutorial Part 1 – How to use Composer

  1. this feels a lot like npm for javascript projects. so where can learn more about packages available through composer?

  2. It's always easy to pull package for a new project but way too hard for adding one with old and existing projects

  3. Need lesson/tip on using composer for an old project. I faced a lot of issues with composer if the project/website is old and need to install a new plugin. Dependencies never seems to met and it makes impossible to add new plugin. Sometimes old plugins aren't available anymore and I am forced to use newer ones for which I have no idea how to make sure to get right packages and update the older ones.

  4. Thank you Reval! I have looked for DAYS for an example of what it actually looks like to install a package using Composer, seeing what is going on. This embodies the detail of the relationship between the local server, composer, what the hierarchy looks like after it's installed, WHERE to install it in the first place! THANK YOU. Excellent work!! You kept it to a bare-bones example that provides such valuable clarity for a newbie. Thoughtful presentation, greatly greatly appreciated!!! You a-b-s-o-l-u-t-e-l-y ROCK!

  5. Just watched first composer video. As someone who kept looking at composer and having it fly right over their head a few times, this was really good. It actually went to the necessary level of detail instead of assuming users having familiarity with miscellaneous subtle details that are often left out.

  6. Great video tutorial but many work on hosts. Setting up a localhost using XAMPP or WAMP and using Composer is a lot of unnecessary BS when you can usually just add one line of code to include a library. Composer is some sort of weird cult. Anyone who really needs Composer is probably facing dependency hell.

  7. in wamp localhost when i use via cmd then composer not work but use php strom terminal then work perfectly. what is reason?

  8. I've got a shared hosting service for my website, so I don't have access to WHM and can't run commands like you did.
    Can I still use composer?

Leave a Reply

Your email address will not be published. Required fields are marked *