Composer PHP Tutorial | Beginner's Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to the second episode of humbrol tech tips and in this episode we are going to discuss about composer and what is composer why it is used what are the benefits and why the developer psp developer on on this dna should strictly be using composer to manage the dependencies so so before we start discussing about composer let's discuss what's dependency management because composer is basically a dependency management and every modern uh programming languages has a dependency management uh psp back in the days didn't used to have and we used to struggle with managing the dependencies and it was a big downside for php in general as a programming language so if we need to talk about dependency management let's understand like you know any projects would not exist in isolation so let's say you create a couple of classes for your projects to work so your projects is dependent on those classes for it to function so that dependency is what you built on your own but then let's say you want to you want to write a write a function uh but then is using third-party uh packages or modules on on on your on your projects so that's another external dependencies that you'll be using on your on your project so no projects is like you know isolated or standalone on its own uh you got to use different uh different packages different modules for your projects to work because of the fact that you are not supposed to reinvent the wheel if it is already there so that's that's the entire concept about this whole dependency management about you are using the packages or portion of the of the of the features that that has already been done and you write on top of that to you know get your get your logic out there and make it make it work that way your entire process of building the project would be quicker so it saves you a lot of hassle and helps you to finish the project faster so that's how how that's how what's the pro dependency management is so dependency management actually basically is a process of managing all your internal and external dependencies upgrading them updating them removing them and that sort of stuff and basically helps you to like you know get your project done faster so that's what dependency management is and like i like i we previously said psp is dependency management is composer so composer is not to be mistaken as yum or apt on linux which is a strictly a package manager but composer is more of a package manager but it is more of a per project basis so so in that context is more like nodes npm or ruby's bundler and php 5.3.2 or onwards is required to run a composer so that's basically a brief summary about what composer is and if i if we need to talk about like how composer works it maintains a a simple session file known as composer notation on a project's root directory which has different nodes for instance the required nodes have all the packages name and its version details that your data composer requires as the dependencies and have other other nodes like name description and that sort of stuff which tells you more about why what the projects is and who's the author and that sort of details so to install the dependency you do composer install and package his name or if you are into already built projects but just want to install the dependencies as a deployment process you just import the files or transfer the files and do composer install to install all the packages that's mentioned in composer citation file and basically when you are writing our codes uh you you composer also autoloads all the dependencies so you are not you are not required to you know manually include all the files that you require so uh composer generally once you do the setup it generates the auto load file and you once you require the auto load file on your projects it would automatically make all the packages that you require that you make it required on a composer.json file would uh basically be available for you to use so you're not you're not like you know so required to include all those files so that would save you a lot of a lot of management so that's one of the another benefits of using composer and basically packages.org arc is a is a is a website where you can uh know and search about the various packages for composer so so like we like we briefly touched based about auto load on a previous slide so basically auto load would allow you to you know uh include the file without required and include on on the on the programming so it is very powerful and neat uh neat feature uh and one of one of the key reason why you would be using such dependency management uh so basically uh auto load nowadays on a a modern uh modern versions of psp is psr4 compliant so they are basically our standards for uh various functions you perform so there are different types of like you know auto loading methods uh basically four to be specific so we have class map psr4 psf 0 and files so class map would be you know you would be manually uh declaring about what classes you would be autoloading so basically you required a key value pair of your your classes psr0 is basically a old uh psr convention for autoloading your classes which is replaced by psr4 now and files is uh one two psr uh zero ps or full class map they're all basically our class um classes php classes auto loading but files would be autoloading of files no matter uh whether it's uh it's a classes or not so let's say your projects have a helper helper file which includes like bunch of you know uh simple mini functions and you would need them throughout the projects so you could easily like declare that on the files on for the autoloading property of uncompensated education and that would be available for you throughout the projects without the need to like include them manually so auto load auto loading is like you know shown here uh would be again like done on composer suggestion file so some of the very you know quick but useful uh commands for running composer would be composer require and the vendor names as package name so this would be generally convention about how you name your composer packages uh composer so would so all the available composer uh packages uh you you can install composer outdated composer updates so these are all the tools are commands that you can use uh not all but the key ones that you would frequently use on your project so for instance if you need to install uh installer projects you do composite install and then you know uh define a vendor name and package name or basically a package name uh if you want to uninstall a package you do composite remove and package name uh uh if you need to like you know once you have finished uh composing a composite suggestion file you do composer dump autoload so that would generally create autoload file that you could included in a project so these are some of the key uh commands we will basically use those like uh in our in our practical session as well so but then this would be good to like keep it memorized for to use the composer while you're working with the projects so now let's uh let's do a quick projects to all practically see everything we have discussed here so basically let's do a small like you know we designed a projects project so let's do a small uh document crawler to fetch the news from uh from any given website in this case let's use washingtonpost.com and try to like fit all the headlines for the uh on the on the homepage of washingtonpost.com and display on a list so for that we would use uh we set up a dependency on a uh for for symphonies dom crawler and css selector so these are uh these are the these are the third party packages we will use in our composer and auto load them and also we write our own class and auto load them with psr4 convention and then basically use that to to show the so the uh list of like headlines and washingtonpost.com on our on our projects so this would be our so i have a folder called practice that has a virtual host set up for test.local and let's create a folder here for our practice projects called composer and let's open up a visual studio code so this is what i use as a as ide so in here let's create psp so so that's what we set up and composer this is where vx is sorry yeah this is where you access uh access the file so nothing fancy going on right now so let's set up a composer here so basically to set up a composer uh i'll be using a integrated terminal here from now on so to get started with the composer uh basically we had we had a bunch of like you know uh a small uh sneak peek about like various commands so we'll be using these commands so basically composer init is one of the one of the first function that you would initiate to start a and start your composer project so composer in it and let's say it would generate a config name for you so this would be uh this would be a project name for you uh let's go with the details here uh basically you would mention all your details here while you are writing for uh for a project so once you do that and accept all the default values so basically you would you would write your author name your description your project license if there are any dependencies you could also uh go ahead and do it search the search the package here from here but basically we'll do it on a on a stepwise basis but just i just wanted to show you like you know this is a composite suggestion file that this command uh created for us so it has this for now this name and empty required uh required node so this is the suggestion file that has all the instructions about about our projects as of now so so once as as we have like you know uh discuss on what our projects would be uh basically we would be we'd be requiring to get uh all the you know uh headlines for from the washingtonpost.com so to achieve that what we have planned is we'll be using symphony components so symphony has bunch of very neat uh if you go to symphony website and look for the components it has various bunch of you know cool components for you to free to use so they have like dependency management error handler and many other projects which you can use on your creative projects uh for for you know easier and quicker implementation of various features so in this context would be using our dom crawler so it would helps us to navigate the dom uh quicker for us a selector so css selector would work in conjunction with dump color crawler sorry uh but then uh it would have helps us to easily like if you go to its documentation and see uh it would easily help us to you know map the x path of the of the various you know css like selector uh so it works in conjunction with dum dum crawler to for us to select different dom elements so the idea would be to you know uh inspect what different css classes that washington post is using and grab its you know grab it vias the css selector uh from from the dom that we get from the from the url so that would be the idea so for installation if you look at the documentation uh you are required to run composer require symphony dom color so this is the this is the name of the package require is uh is a composer you know uh requires a composer command to to install the package so let's grab it and install it on our on our projects composer requires symphony dom crawler and another would be to get css selectors so again we'll be using so let's wait for this to complete and would be uh composite requires simple initiative selectors so these are two of the dependencies from the symphony uh component that we're using so now if you go to composer.json file you would see the required attributes is updated and you have the two packages listed here so now our project knows that we have these two you know dependencies that we that we are using for our projects so so apart from that now basically what you need to do is to include these to uh to start using this pro uh dependencies uh how you how you do is you know composer dum auto load so the dump auto load would would uh actually you know it would read the composer.json file and create a autoload.php file for you on a vendor folder so the vendor folder was created from a composer where you have all the dependencies listed out here uh faced from the from the command line for you ready to use so you didn't need to manually you know worry about like you know uh where to include that or what to use uh basically you just include this autoload.psp file on your on your projects and you would be able to you'd be able to you know uh use all of those dependencies that you have in your project so basically what you do is include uh let's say include one's directory and it's inside of render sorry vendor folder and autoload.psp sorry so this is this is basically what lets you shut up uh uh autoload so this is this is just just one line where you include the autoloader php from the composer directory and you would be able to use the packages for instance css selector this is one one of the one of the packages we use so you'd be able to use be able to use those packages on your projects now on because you have already you know included them as afterwards so so as a part of a project setup this is this is uh this is one setup we have done for all our you know external dependencies but then we also have a requirement where would be you know as a part of project planning what you've done is we'd also include psr4 auto load of our own css classes as well so that's what we plan uh so let's create a source directory and in here let's have classes so this is where you know for instance let's say news dot psp so so this is this is this is where this is our our class and let's say uh let's say we have we we want to you know uh have this also auto load as a part of our projects because this would be our internal dependencies on our projects we'll be using it from index.php because we don't want to mess this main entry point of our projects with all the codes and want to go with object-oriented programming languages route instead so if you if you are to use such features from compo composer where you would be loading your own uh own dependencies as well you would load that with a psr4 standard and this is nothing then just uh you know another uh node where you define the autoload and you would write a psr4 convention where you would define where to look for this so let's say i have a name of my color and where do i look at look for we'll look for stores so my clutter this is this is the name space for my for my uh for my projects so all the classes would be inside the my crawler naming space so if you're not sure about the naming space i will make it make another video about that in the future as well but basically our namespace would be a very efficient way of you know organizing different classes so let's say there are there are news classes for another like projects where you'd be in uh including that as well so that news would not conflict with our crawler news where new name space would come in handy because it is my crawler's news not any other packages news so that's what basically this stuff about namespace is and where we look for the files would be on the source directory so this is basically a very simple uh setup for setting up autoload for our for our custom like you know uh classes for our projects so once that's being done what now what do we need to do let's let's define a let's define a crawler and sorry news class so in here basically at the very beginning you would have to define a namespace and namespace would be whatever name space you have as a root namespace and this is inside a classes folder so this is basically a namespace that you will apply for for our for our projects and now let's have a class news and in here the idea would be to you know let's have a let's have a constructor here so i'm sure you would know the details about what constructor and basically what the object how object-oriented programming language works in general uh so i will i don't bother to like you know explain these in details over here but obviously we would uh we would include those on our future sessions if that's required as well so in here as a constructor let's accept the argument where url would be passed uh so in here the idea would be to you know get the washington post url here uh and that would what would use here to use you here use here to crawl the sites so let's have some you know private variables here uh so url to call uh let's do blank for now and dome would be also blank for now and then crawler and let's grab a headline so this is this is what i intend to use on a project so where i store the url that i need to crawl where i store dom that's uh that's a that's a actual like you know html content from the url and crawler would be a symphony symphony components against that dom where we could you could grab different headlines there and headlines would be the the array where the crawler would basically return different headlines here and we we nicely you know uh implement this as an array in return for us to use so that's the idea let's see this can change obviously based on like you know how it goes but for now the idea is to to work it with these variables so right on the go let's set this to url and dom would be let's use psp you know get content let's start url to crawl so right off the bat you know uh we would have this dumb now has uh has all the html uh html for this too for this to work if you need to if you need to you know see how this is working so far let's return it and now in here and let's composer dump it first and now let's use uh my color classes slash news so this is what we have we are using a news and now let's say news dom would be new news and what would be the argument would be the url that we have done here so let's see how so far it's working so we have basically we are passing a news um arguments where the url of the post that uh url of the site that we are trying to crawl and on the news uh we are getting our html content of this and just returning so nothing fancy as of now but just like you know checking in if we are in the right track so far so it takes a while because it's actually crawling the site and extracting the dom uh from the side so basically this is this is what we have uh so if you if you see the source we have actual you know html from the from the page so so far it's looking good but this but this is not what we need we would basically need to cr we basically will need to be able to you know uh extract different different uh different like tags out out here so let's quickly go to their component documentation and see how we are how we are how we can use it so basically first of all right of the bat you need to you need to you know use the uh crawler component here and basically you just create a uh basically you just create a crawler object for that where you would be able to you know navigate through different terms so as of now let's uh assign new color and this would have the term so that's what we have so far and now let's not like you know do everything in a constructor but then let's create another function where to get headlines right and nothing fancy in here no arguments as well so what what headlines would do is like you know uh on on this color object would basically just filter all the headlines uh on a on a dom element so let's quickly go in here on the side and see uh they have basically applied headline classes to two different uh to different headlines so yeah if we if you if you if you are able to you know just grab the headlines and anchor tag to the headline i think i think would be able to get all the other headlines of the projects here so so what we'll basically do is like first of all let's check we have a crawler in here not so that would be the first thing if not then we'll just return false now if we have the if you have the crawler objects uh basically for now let's uh headlines as a empty array for now and now if we now if we go to a documentation about the dom crawler you can you can basically use a use a filter method here uh and hold on so and then you can also use the anonymous function where you can loop through different different filter clock colors over here so as of now let me quickly grab this so this would be the start color we're not filtering a p but headlines a and it would require a cl traveler object with nodes so this is what it uh expects as an anonymous function so uh so that would require us to get us the all the all the you know headlines inside this function so this is based strictly based on the documentation that we have for this component over here so let's not return but let's say uh this dot headline would would have a id of let's say text would be text so yeah and at the end once the loop is completed we'll just return this to that uh return this so basically what what it is doing right now is this uh crawling the sides and then filtering the headlines a and looping through that and extracting all the text out of this so in here let's say let's grab a let's grab a headlines now but it's again from a news dom and here get what was the name of the get headlines so get headlines so let's now pad on the headline and see what we get so yeah it takes a while because you're actually you are going in the internet and getting back 30s so we don't have any result in here let me check what's wrong a headline oh i'm sorry i think that's singular over there so let me refresh and see so yeah so you have all the all the headlines over here so now uh i think that's that's pretty much what we want to do uh let's also grab the grab the link for the news so for that again if you need to see the so yeah we can grab a attribute with attr value so let's use that uh and we want href so this would get us all the links and the texts let's see so yeah we are getting all the links and the text as well so now let's quickly you know template this uh a little bit nicely so we're not doing a real templates but just something very basic so that we don't clutter this too much so say news news based psp and this is where you do news nest uh too quickly you know let's use bootstrap here so that our list do not look very ugly so something will not we're just using a html i mean css for this so in here let's use a container and then hold on i think bootstrap has a list component over here so yeah this is what we are using in here but here let's say latest so that's what is being said in here let's simply include the template is this of course uh please don't do such kind of templating and a production uh project so this is just for the sake of like you know quickly showing you the result about how we can display these headlines uh so we didn't clutter this this index.php file too much so since we have uh include here and headline we didn't need to like you know real templating engine pass down the passed on the context or the variables there because it's since it is included it would automatically be available here so let's say if our size of this is greater than 0 will actually be including this so this doesn't produce any notice of warning if there's nothing in here and on a headlines we simply do for each headline as h let's get rid of this so on a forage uh basically all you need to do is echo edge and what was how how was our area constructed so link and text right so link would be at link and text would be text in here so that's what has been done and being used so yeah i i think it should it should get us to a nice little list from washington post of all the lettuce headlines over here so yeah as you can see we have all the headlines being crawled from the washington post website where we had used different symphony components so if we had to click on the different headlines you would be taken to that taken to that news articles so this was a very quick but i hope give you some context about how we were able to set up a composer we're able to uh set up our own auto load and create our own classes where different like dependencies into a symphony component were uh implemented and we're crawling the sides using various like component or packages related functions so basically this this is this is this is uh a very quick uh and dirty way of like you know showing how component composer can be used on a project so if i need to go back to you know uh to to to to our slide again uh what we can say is how how how this actually the entire thing worked is you know when we did a composer in it it created a composer.json file and when we dumped the compositization file it created a vendor and autoload the php file so when we are like including autoload.php file uh it would it would automatically load all the uh vendor uh requires that we have and also all the psr for like you know custom classes we have done for our own projects so they all work in conjunction and your like index.php now has all the all of these available so where you would be able to use all the functions and classes and and get your projects done you were able to you know use those packages and create the projects on your own unrelatively short period of time so so yeah this is this is how we use a composer and i hope it helped you a bit to give you some idea about and context about how this whole thing works and together if you have any questions yeah feel free to let me know and i'll try to you know get back to you whenever that's possible so yeah thanks have a good day bye for now
Info
Channel: Hamro Tech Tips
Views: 2,341
Rating: 4.9069767 out of 5
Keywords: composer, php, php composer, programming, hamro tech tips, dependency manager, dependency management
Id: mcHQYHsr5OA
Channel Id: undefined
Length: 40min 34sec (2434 seconds)
Published: Sun Sep 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.