Transform VS Code into a Formidable PHP Experience

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this layer a bit i'm going to show you a few tips and tricks for how to use vs code effectively and efficiently as a php developer visual studio code is a powerful and versatile editor but out of the box it really doesn't have a lot of support for php code bases while we might not be able to make it as robust as a phpstorm we can get close enough for a lot of use cases so i have here a basic code base it's a laravel app out of the box and while this video doesn't pertain to any one framework in particular i work in laravel the most so this is what i'll be using but all of these tips and tricks will work for any php framework cms or just vanilla files so we have this controller here post controller and this makes for a good playground to show off the tips and tricks that will follow the first one that we're going to install is called php intellifence it's a package from the visual studio marketplace and if we take a look at it it's pretty wildly popular now there is an alternative to this called php intellisense it also was just as popular but is now unpublished from the marketplace intel offense is the one that's currently supported by the community and works the best with vs code what it does is add a suite of useful php features uh code completion go to definition support finding references symbol searches but let me show you how to install it and how it works so if we open up vs code again we go to our extensions area we can type in php intellifence and click install and once installed it should be good to go you don't have to restart or anything depending on your code base size it may take some time to actually index your entire code in which case you'll see it here at the bottom next to the problems area if you have the status bar open ours isn't indexing anymore which means that we should be able to use all the features right now so if we go back to our code open up our post controller again we can do something like this so if we take an array we should now be able to do something like an array and we can get a definition of what that function does the arguments that are needed for it as well as the types associated with each of those arguments and sometimes a basic description of what it does so we can say an array needle let's do my posts is in posts and that returns a boolean so we can do is in post equals and there we go and now this is you see our is in post is grayed out because that variable is not used in any code further down the line but if we did something like a comparison on it if is in post you can see it lights up there and we also get a basic error handling if we mix something up in a php function so if we remove this second argument here we get a squiggly red line and if we hover over it it's expecting two arguments but it only found one we could view the problem and it just highlights that in a brighter area we also have go to definitions so if we do something like public posts here at the top and create a constructor and then we reference that somewhere down the line we can press ctrl or command if you're on mac and hover over the posts we then see a definition for that property and clicking on it highlights it at its start one more useful feature about intellifence is that it also does code completion and intelligence for embedded html css or javascript inside of php files so if we were to open up something like a view file which ends in php you can see that it's already picking up something and if we go to focus problems in vs code we have one notice that says property is ignored due to the display with display block and if we double click on this it automatically highlights this problem so we could get rid of it and solve the issue it also has completion as well so if we added in a line here like form we have it pre-populated with the action and it will even do suggestions for things like the method or ink type and adding a button we can see the javascript completion and we can see that we even highlight this error here because we added in an extra parenthesis all right the next package that we're going to install is for composer so if we open up the extensions and search for composer we want this third one right here composer companion now you might be thinking why not use the one that has way more downloads i've used it before and there's a lot of problems with it uh the require commands don't really work there's really not a whole lot of added functionality that you couldn't just do in your command line and it seems to be kind of broken at the moment so i found this one as a replacement and it seems to work beautifully so we'll install it here and go back to our application and before we can use it we'll have to set a couple of settings so let's go to our settings json and at the bottom [Music] we'll use composer companion enabled as true and composer companion executable path to the path of our composer executable which if you need to find that we can just open up a terminal and type which composer so that is user local bin composer and now back to our application we have a new window here at the bottom called composer scripts and we can directly run scripts that are in our composer file by clicking this play button here next to each one of them i don't need to do that right now but what i would like to do is add a new package to my application if i open up the command line and type in composer we now have a whole list of commands that we can use at our disposal without having to open up a new terminal window so i can go to require and it'll ask me for a vendor and a package as well as a version which is optional let's do fruitcake laravel cores and it'll ask me for any additional options i don't want any of these so i can just hit enter and it ran the command right in my terminal so it added the package it ran the post auto load dump script and you can see there now it is discovered in my application all right let's try that one more time let's try clear cache and it cleared it immediately okay so that's a composer package let's go on to another one for the third tip i'm going to use a package called php namespace resolver if we take a look at it in the marketplace it has quite a lot of installs and you'll see why shortly this makes it really easy to add namespaces into your application and it has a variety of commands that we can use which come in handy alright let's install it so show our extensions again and php namespace this first one here and after it's installed we can start using it immediately so let's head back to our application and let's say i wanted to use the user model here so i could say user and it will automatically find my app models user and if i click that it added in the namespace use statement up here automatically now what if i had another couple of models let's say a post and a comment let me just create those real quick and we'll add these to our test method users and each time that i hover over one of these models whenever i start typing it it adds the namespace at the top of my application we have comment post and user and it also adds them in alphabetically but if we wanted to we could reorganize these using the commands we could say sort imports which now sorts them by length and then alphabetical so because post and user are the same length post comes first and user comes second we can also highlight each of the ones that aren't used so if we remove this post call here and we said highlights not use classes both the post and the request class are highlighted until we go to use them alright this tip was pretty short as is the next one we're going to use something called php constructor let's open up our extensions and find php constructor and install it now this one really only does one thing but it does it pretty well and it saves some time let's open up our post controller again and in the command line let's type in constructor and you'll see this command insert constructor property if we hit that it now adds in and pre-populates a protected property as well as a constructor for the class it also has all the property variables highlighted so we can easily replace them all at once so if we wanted to we could do something like this and replace them all with users so now we have a protected property called users and it's initialized each time that the class is called by default it uses the protected attribute but in the settings we could change it if we wanted to so if we start typing constructor we can see php constructor choose property visibility which means that we can pick it each time or if we wanted to set it as something automatically we could choose visibility and have it be either private protected or public i want to choose it each time so i'm going to keep this attribute true now if we go back and run that same command again it tacks it on to the same construct method so now we can say posts protected was highlighted so it should be able to be edited out but i believe because i'm using my vim extension here it didn't work out as well as i intended it to so for now i think i'm actually going to change my settings and have this set all the properties immediately to public okay so using it once more we see that the property is automatically public and i can change this to comments so with one simple command ran three times i now have a constructor with three separate attributes being initialized and set pretty easily i think i'll do one more tip and for the last one it's something that's going to be pretty useful chances are if you use php pretty regularly you'll have come across xdebug it's a powerful debugger tool and one that has native integration to most php ides it doesn't out of the box in vs code but with a simple extension and some configuration we can make that happen so in the extensions window let's get started with that x debug and we want this php debug debug support for php with xdebug extension all right once it's installed we can go ahead and get started setting it up so let's head back to our application and open up something like our post controller in our index method we're going to remove this and this and we'll just add some cruft code here so it has something to step over let's say test array is an array with uh i don't know some fruit in it banana pineapple cherry orange and then we'll say uh uh we'll say vegetable is uh carrots and is fruits equals in array vegetable test array and actually for posterity's sake let's call these fruits and this test array we'll call fruits again and then at the bottom we'll just spit out his fruit okay and we'll get rid of this user's call too okay so now we can set breakpoints in our code on the sidebar when we hover over using these little red dots so let's set one right at our fruits array and opening up the command line and let's say debug php now that'll bring up this entire debug setup here and kind of take over our ide right now so we have nothing running right now we have nothing going on and i have this index method mapped to slash posts so let's visit it in our browser okay well nothing happened let's actually make sure that we're seeing what we should be here let's say is fruit and let's make a conditional is a fruit is not a fruit that way we at least see something back in our web browser okay it's not a fruit but we're also not seeing our breakpoint hit now i have x debug running as an extension in my local php environment but i need to connect that to vs code in order to make that breakpoint known and stop the code execution there all i have to do is click this play button here next to listen for x debug and it will start the launcher so if i go into my browser and hit refresh my breakpoint got hit and we have all the variables stored out here as well as a call stack for the breakpoint and now we can step over our code to go to the next line and see that our fruits variable now has these four values in it and we can step over again is vegetable or vegetable as a carrot step over one more time is fruit is false and we can continue stepping over and stepping over and viewing the call stack and we can also stop execution and everything goes back to the way that it was before we can remove the break point and we can also if we add in multiple break points we can see them all at the bottom of this window here so we can see that this is at line 11 of post controller this is at line 21 of post controller we can even open up different files and add in breakpoints in them as well and see them all listed out here and then clicking the numbers will highlight the line for the file that it's associated with and we can just remove them all here if we wanted to one quick caveat is that my setup for this is a little different because i'm running my local environment through docker if i go to this gear here it opens up a launch.json file which allows you to set the configuration for each of the x debug profiles that are in this drop-down i'm only using this top one listen for x debug and on my environment i have to add this path mappings here from var www.html to the workspace folder and this hostname set to localhost other than that everything else remains the same alright i think that's it i use vs code for the bulk of my day-to-day php work and it's all possible because of these packages if you have something that you use to make php development easier with vs code be sure to let me know in the comments
Info
Channel: Laracasts
Views: 14,407
Rating: undefined out of 5
Keywords: web development, php, programming, laravel, laracasts, jeffrey way, coding
Id: UwMRmXIFVCs
Channel Id: undefined
Length: 17min 19sec (1039 seconds)
Published: Thu Nov 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.