Topics

Composer Documentation

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

Details :

https://getcomposer.org/doc/

Dependency management

Composer is not a package manager in the same sense as Yum or Apt are. Yes, it deals with "packages" or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it does not install anything globally. Thus, it is a dependency manager. It does however support a "global" project for convenience via the global command.

This idea is not new and Composer is strongly inspired by node's npm and ruby's bundler.

Suppose:

  • You have a project that depends on a number of libraries.
  • Some of those libraries depend on other libraries.

Composer:

  • Enables you to declare the libraries you depend on.
  • Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).

Install composer

Download composer for windows and install it. then make your project folder and make one file composer.json then composer update.

composer.json

{
	"name": "mytesting project",
	"require":{
		"monolog/monolog": "1.0.*"
	}

}	

Note: composer update command : composer update

require-dev

The require-dev packages are packages that aren't necessary for your project to work and shouldn't be included in the production version of your project.

 {
	"name": "mytesting project",
	"require":{
		"monolog/monolog": "1.0.*"
	},
	"require-dev":{
	"illuminate/support":"5.6.*"
	}

}	

Download all packages for composer.

Go to Packagist site and download your packages.

Download your packages.| click here..

Refresh composer.

Note: composer dumpautoload



composer remove

Note: composer remove (packagename)



How to create Project with composer

Note: composer create-project illuminate/events event(project folder name)


Include all file using composer

include all file using psr-4

{
	"name": "mytesting project",
	"require":{
		"monolog/monolog": "1.0.*"
	},
	"autoload":{
		"psr-4":{
			"App\\":"app"
		}
	}

}


Note: file directory list
app->controller->userController.php //namespace App\Controller;
app->model->user.php  //namespace App\model;


use index page
$user = new App\Controller\userController();
$model = new App\Model\user();

Without namespace you can load files

without namesapce you can load all files useing files commend

{
	"name": "mytesting project",
	"require":{
		"monolog/monolog": "1.0.*"
	},
	"autoload":{
		"psr-4":{
			"App\\":"app"
		},
		"files":[
			"app/functions.php",
			"app/function1.php"
		]
	}

}


Note: app is folder name and functions.php and function1.php is page name.


folder All class load

folder all class load using classmap

{
	"name": "mytesting project",
	"require":{
		"monolog/monolog": "1.0.*"
	},
	"autoload":{
		"psr-4":{
			"App\\":"app"
		},
		"files":[
			"app/functions.php",
			"app/function1.php"
		],
		"classmap":[
			"app/classes"
		]
	}

}

Note: app and classes folder name

Some class example

    class Class1
    {
    	
    	function __construct()
    	{
    		echo "class 1 mgs";
    	}
    }
    
    using 
    ===================
    $c = new Class1();

Subfolder class example

namespace Any;
class Class3
{
	
	function __construct()
	{
		echo "class 2 mgs";
	}
}


using
=============
$cc = new Any\Class3();