Composer's Tags

How to create a PHP package for Composer
PHP has become more popular programing language in website development. Composer is the one of the major key factor. Sometimes you need some functionality work that you already worked one project. Or when you search in the web, most of the time, you will already find that in github repository. In that case, you just have to download that library with git and put into your project, sometimes need to add your configurations and you can use that library in your poroject with composer. Composer is the PHP package manager by which you can use packages developed by community. You can also create your own package and share it with the other developers. In this tutorial article, we will create simple package and use it. I will create simple project which will provide IP address of the user. composer.json file To create package, first you need to initialize your package with composer.json file. composer.json is the file where your packge information is stored. To create composer.json file, run the bellow command: composer init When you run this command, it will ask for package information, like package name, description, author, type, licence etc.. It will also ask if your package requires other packages. After that, you can see bellow composer.json file. Here is the file that I have create. {     "name": "jiteshmeniya/ipgetter",     "description": "Get IP address of the user",     "type": "library",     "license": "MIT",     "authors": [         {             "name": "jiteshmeniya99",             "email": ""         }     ],     "require": {} } Other than composer.json, there are also some files in the root of package. file is the basic documentation that will describe about package and how to use it. LICENSE file is terms how other developers are allowed to use and modify package code. src is the main directory where all of your code are reside Now let's talk about code. Create file in the src directory. Remember your file name should be match with class defined in the file. You should create one class in one file. If you need another class, create a new file. Also you can not create multiple class with same name. It is recommend you use new classname for every file. I have created bellow class in src/getip/IPGetter.php file. <?php namespace IPGetter; class IPGetter {     /**      * get IP address of the user      *      * @return void      */     public static function getIPAddress()     {         return $_SERVER['REMOTE_ADDR'];     } } Now we have to modify composer.json file as we need to autoload all of src classes. {     "name": "jiteshmeniya/ipgetter",     "description": "Get IP address of the user",     "type": "library",     "license": "MIT",     "authors": [         {             "name": "jiteshmeniya99",             "email": ""         }     ],     "require": {         "php": ">=5.6"     },     "autoload": {         "psr-4": {             "IPGetter\\": "src/getip"         }     } } In the require, we have added minimum php version reuired. So if your package has code that can only works in specific PHP version, you can specify here. So after that someone will try to install this will get error without install. Also in the autoload, we have added the class namespace, so all the files inside src/getip directory will be loaded namespace "IPGetter". Send package to Packagist. After package has been created, you can share it other developers with github and send it to packagist. By doing this, other developers can use your package with just one command. To upload your package to github, check this article[How to upload project to git?] Now go to Packagist and submit your Github package link. Now you will get command for install package. In my case I get command like this: composer require jiteshmeniya/ipgetter Use package in your Project Whenever you need to use package in your project, run that command in project. It will create vendor directory with with your package folder along with composer folder. composer folder loads all of vendor folders classes. There is also autoload.php file in vendor folder which includes composer folders all class. So all you have to do is include vendor/autoload.php file in your index.php file. I have created bellow index.php file and used package class. <?php require_once __DIR__ . '/vendor/autoload.php'; use IPGetter\IPGetter; $ip_address = IPGetter::getIPAddress(); echo($ip_address);exit; Conclusion In this way, you can also create dynamic and complex packages according to your requirement and use in your all projects. This way it also helps other developers to use your package and contribute to community. If you have any query or suggestion, please comment bellow.
Remove a Package from Laravel using Composer
Composer is dependency manager for PHP packages. Laravel also uses composer to install and remove packages from the application. In this article, we will see how to remove package from your Laravel application. Actually you can remove package from any PHP application which uses composer as package manager. Run the following command and it will remove the package from the application. It will also update composer.json and composer.lock automatically. composer remove vendor/package After that, you will also need to remove references to that package within your application, from config, controllers etc. otherwise it may give class not found error because you have removed all class files from application.
How to add or remove package in Laravel application
When you upgrade your Laravel application or doing changes in appliction, then you might want to add new package or remove pacakge from Laravel application. Laravel uses composer package manager to add or remove packages. Add package in Laravel You can add new package in Laravel using composer installation command. Suppose you want to install Laravel Passport in your Laravel application. composer require laravel/passport This will add all package files into vendor folder. Your application composer.json file is also updated. Composer manages all package through composer.json file. Remove package from Laravel In the same way you can remove package from Laravel using composer remove command. composer remove laravel/passport This will remove package files from vendor folder and also from composer.json. You can find the pacakges list in composer.json file. The last step is to remove package class reference from your application e.g., from config/app.php or controller file else it will give class not found error.