Last active
January 21, 2018 16:07
-
-
Save rahulcs754/c0a1eab69e8af9eda4fd7e062ab71b9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello Friends! | |
Today we will learn About. | |
"How to autoload PHP classes the Composer way?" | |
or | |
"How to Implement PSR-4 in our project ?" | |
->First, create a working folder in xampp or wampserver directory.I have used xammp server, So i'm Creating Folder in htdocs folder . | |
->I am creating project1 name folder in htdocs. | |
->Open Command Prompt by clicking on start button and search command prompt.Another option press Home key + r and Type cmd and hit enter. | |
-> Go to working directory by using cd command. | |
->example : | |
I am using window 10, the initial path will be | |
C:\Users\name_of_user> | |
My computer name is rahul, so my intial path is: | |
C:\Users\rahul> | |
-> Now ,I am going to working Directory by using this command. | |
C:\Users\rahul>cd / | |
Type cd / and hit enter. | |
->Now , Type cd xampp and hit enter again. path will display like this. | |
C:\>cd xampp | |
After hitting enter. Our path will be like this | |
C:\xampp> | |
->Next, type in command prompt cd htdocs/project1 and hit enter. After hitting enter, path will show like this. | |
C:\xampp\htdocs\project1> | |
->It's time to initial Composer by using this command. | |
Composer init. | |
C:\xampp\htdocs\project1>composer init | |
hit enter | |
->After hitting Enter. It will be asking some information like package name, version, description etc. Change it as per you need or hit enter to use default information. After this, a file will be created in your Project directory named composer.json. | |
->second command | |
composer install | |
->Type composer install and hit enter. | |
->After this, a folder named vendor will be created in working directory automatically. | |
->Now, open composer.json file In any code editor like as Sublime ,Nodepad++ ,PhpDesigner8, Atom etc. And | |
I am using Sublime Editor. | |
->Next Step, Create an src directory in the project folder. That will hold class files. | |
->I have created src folder and created two files, first one is app.php and second is db.php . | |
->Define same namespace Acme in app.php and db.php. Now, write some simple code in app.php like this. | |
Next step: | |
->Add this code in composer.json; | |
{ | |
"autoload": { | |
"psr-4": { | |
"Acme\\":"src/" | |
} | |
} | |
} | |
a. We used The psr-4 key (This PSR describes a specification for autoloading classes from file paths ). | |
b. The namespace Acme points to the src/ directory. | |
c. The namespace has to end with \\. For example, "Acme\\". | |
d. You can replace the generic Acme with the name of your brand or website. | |
->Now, create an index.php in the root directory. | |
-> Update the composer autoloader from the command line: | |
composer dumpautoload -o | |
->C:\xampp\htdocs\project1>composer dumpautoload -o And hitting Enter. | |
-> Set Path in your autoload.php by using Above command. | |
-> Add a file autoload.php in index.php like this: | |
require "vendor/autoload.php"; | |
->Now, calling file using namespace and creating an object. | |
->Finally, i will show you how to add or run autoload composer code on my system. | |
Thank's for watching. | |
Don't forget to like & Subscribe. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment