{"id":1583,"date":"2017-07-25T11:15:22","date_gmt":"2017-07-25T09:15:22","guid":{"rendered":"http:\/\/www.kavdesign.net\/blog\/?p=1583"},"modified":"2017-07-25T11:19:02","modified_gmt":"2017-07-25T09:19:02","slug":"a-short-simple-composer-tutorial","status":"publish","type":"post","link":"https:\/\/www.kavdesign.net\/blog\/coding\/a-short-simple-composer-tutorial\/","title":{"rendered":"A short &#038; simple Composer tutorial"},"content":{"rendered":"<p><img decoding=\"async\" class=\"attachment-l size-l wp-post-image\" src=\"https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1.jpg\" sizes=\"(max-width: 360px) 100vw, 360px\" srcset=\"https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1.jpg 360w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1-80x80.jpg 80w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1-152x152.jpg 152w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1-300x300.jpg 300w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-logo-1-100x100.jpg 100w\" alt=\"composer\" \/><br \/>\nIn this little Composer tutorial we\u2019ll walk through the absolute basics of Composer, the PHP dependency management tool. Composer has changed the way PHP applications are built, and you should definitly take some minutes to get into this handy little thing.<br \/>\n<!--more--><\/p>\n<h2>What does Composer do ?<\/h2>\n<p>In the PHP world (and nearly every other language too) there are solutions for most common tasks, most of them available for free as open-source projects. So if you want to create professional HTML mails with PHP, you\u2019ll probably not write everything by yourself from scratch, you\u2019ll search for a mail library, in this case obviously PHPMailer, and put it into your project. And here Composer comes in: Instead of downloading the chart library by hand, moving it by hand into a manually selected folder, including it by hand and maybe messing around with autoloading issues, Composer organizes this, and everything Composer needs is ONE line of code, something like this:<\/p>\n<pre class=\"prettyprint\">\"phpmailer\/phpmailer\": \"5.2.*\"<\/pre>\n<p>This simple line will add the latest version of the 5.2.x-branch of PHPMailer to your project \u2013 in the most cleanest and updateable (!) way possible. Composer will also check if your current server setup fits the minimum requirements of PHPMailer 5.2.x, so let\u2019s say this version needs PHP 5.4 but you have PHP 5.3, then Composer will give out a big fat warning. Composer will also automatically download all necessary dependencies of the specific library. You\u2019ll be able to use PHPMailer everywhere in your project, without further including issues. When you see a new version of PHPMailer comes out, you can update everything without breaking anything with just a simple action on the linux (or windows) command line.<\/p>\n<p>&nbsp;<\/p>\n<h4>A real example<\/h4>\n<p>To install Composer (on Ubuntu, Debian, CentOS, etc. or Windows 7\/8) follow\u00a0<a href=\"http:\/\/www.dev-metal.com\/install-update-composer-windows-7-ubuntu-debian-centos\">this tutorial<\/a>.<\/p>\n<p>Once Composer is installed, move into your web root and create a new project folder (to keep things clean and not mess into other projects), something like:<\/p>\n<pre class=\"prettyprint\">\/var\/www\/myproject\/<\/pre>\n<p>Create and empy file in the\u00a0<em>myproject<\/em>\u00a0folder, called\u00a0<em>composer.json<\/em>:<\/p>\n<pre class=\"prettyprint\">\/var\/www\/myproject\/composer.json<\/pre>\n<p>Edit the composer.json, put this into the file and save it:<\/p>\n<pre class=\"prettyprint\">{\r\n\u00a0\u00a0\u00a0 \"require\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"raveren\/kint\": \"0.9\"\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>Note the syntax! As the file extension says, it\u2019s a simple\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/JSON\">JSON<\/a>\u00a0file. This file contains structured data that say Composer what to do. In this very simple example we simply want to include the wonderful\u00a0<a href=\"https:\/\/github.com\/raveren\/kint\">KINT tool<\/a>\u00a0into your (empty) project. KINT is the massivly improved version of var_dump() or print(). Now make sure you are in your project\u2019s folder (var\/www\/myproject for example) and tell composer to download the stuff we just declared in the composer.json by doing:<\/p>\n<pre class=\"prettyprint\">composer install<\/pre>\n<p>The result will look like:<\/p>\n<p><a title=\"\" href=\"http:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-install-successful.png\" data-rel=\"lightbox-0\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-707\" src=\"http:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-install-successful.png\" sizes=\"auto, (max-width: 342px) 100vw, 342px\" srcset=\"https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-install-successful.png 342w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-install-successful-270x114.png 270w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/composer-install-successful-300x126.png 300w\" alt=\"composer-install-successful\" width=\"342\" height=\"144\" \/><\/a><\/p>\n<p>Note that\u00a0<strong>Composer has created a folder named\u00a0<em>vendor<\/em>\u00a0in your project<\/strong>\u00a0and downloaded KINT (and other stuff) into it! Don\u2019t touch this folder, Composer handles everything. Let\u2019s create an index.php in the project\u2019s root, open it and put this in the file (and save):<\/p>\n<pre class=\"prettyprint\">&lt;?php\r\n\r\nrequire 'vendor\/autoload.php';<\/pre>\n<p>This loads the Composer autoloader. So this line includes everything that Composer has downloaded into the project.<\/p>\n<p>Let\u2019s have a look onto the\u00a0<a href=\"https:\/\/github.com\/raveren\/kint\">KINT GitHub page<\/a>\u00a0to get an idea on how to use KINT: The tools offers a very short and really cool debugging function\u00a0<strong>d()<\/strong>\u00a0! So with the above\u00a0<strong>require<\/strong>\u00a0line in our index.php we can use stuff loaded by Composer directly, like \u2013 in this case \u2013<\/p>\n<pre class=\"prettyprint\">d($any_variable);<\/pre>\n<p>I like! Okay, let\u2019s try this out in our mini example. Create a demo data array and then echo it out via KINT, like this (we are still in the same index.php):<\/p>\n<pre class=\"prettyprint\">&lt;?php\r\n\/\/ load Composer\r\nrequire 'vendor\/autoload.php';\r\n\r\n\/\/ create demo data\r\n$variable = array(1, 17, \"hello\", null, array(1, 2, 3));\r\n\r\n\/\/ use KINT directly (which has been loaded automatically via Composer)\r\nd($variable);<\/pre>\n<p>Open this file with a browser now. The result should look like:<\/p>\n<p><a title=\"\" href=\"http:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output.png\" data-rel=\"lightbox-1\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-708 size-full\" src=\"http:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output.png\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" srcset=\"https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output.png 520w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output-270x138.png 270w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output-300x153.png 300w, https:\/\/www.dev-metal.com\/wp-content\/uploads\/2013\/12\/kint-output-400x204.png 400w\" alt=\"composer tutorial\" width=\"520\" height=\"265\" \/><\/a><\/p>\n<p>This looks and behaves so much better than\u00a0<em>var_dump()<\/em>! So what have we just done ? We have included a full external php tool with one line of code in the composer.json (and with a one-line-autoloader in the index.php). The external tool is instantly ready to use. We have included KINT in a very clean and commonly accepted way.<\/p>\n<p>To add another external \u201cdependency\u201d simply add another line into the\u00a0<em>composer.json<\/em>\u00a0(keep the JSON syntax in mind, especially the comma in the end of the lines!):<\/p>\n<pre class=\"prettyprint\">{\r\n\u00a0\u00a0\u00a0 \"require\": {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \"raveren\/kint\": \"0.9\",\r\n        \"phpmailer\/phpmailer\": \"5.2.*\"\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>This will add PHPMailer in the latest version of the 5.2 branch to the project. To get this, run<\/p>\n<pre class=\"prettyprint\">composer update<\/pre>\n<p>from the folder where your\u00a0<em>composer.json<\/em>\u00a0is. Usually when working with Composer we do a \u201ccomposer update\u201d or a \u201ccomposer install\u201d, depending on the situation. To get an idea what\u2019s the difference between these two, have a look into\u00a0<a href=\"http:\/\/www.dev-metal.com\/difference-composer-install-composer-update\/\">The difference between \u201ccomposer install\u201d and \u201ccomposer update\u201d<\/a>.<\/p>\n<p>This article was written quite a while ago (4 years), please keep this in mind when using the information written here. Links, code and commands might be outdated or broken.<\/p>\n<p>Article from https:\/\/www.dev-metal.com\/composer-tutorial\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this little Composer tutorial we\u2019ll walk through the absolute basics of Composer, the PHP dependency management tool. Composer has changed the way PHP applications<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/www.kavdesign.net\/blog\/coding\/a-short-simple-composer-tutorial\/\">Continue reading<span class=\"screen-reader-text\">A short &#038; simple Composer tutorial<\/span><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[9],"tags":[436],"class_list":["post-1583","post","type-post","status-publish","format-standard","hentry","category-coding","tag-composer","entry"],"_links":{"self":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/1583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/comments?post=1583"}],"version-history":[{"count":5,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/1583\/revisions"}],"predecessor-version":[{"id":1588,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/1583\/revisions\/1588"}],"wp:attachment":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/media?parent=1583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/categories?post=1583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/tags?post=1583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}