PHP Composer – some commands and tricks

Directory structure:

|   composer.json
|   composer.phar
|   index.php
|
+---app
|   +---controller
|   |       Home.php
|   |
|   \---model
|           FirstDb.php
|
\---vendor
    |   autoload.php
    |
    \---composer
            autoload_classmap.php
            autoload_namespaces.php
            autoload_psr4.php
            autoload_real.php
            autoload_static.php
            ClassLoader.php
            LICENSE

1. Standard case – with namespaces

root/composer.json

{
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

root/app/controller/Home.php

<?php
namespace App\Controller;

class Home{

	public function index(){
		echo "Hello from Home->Index!";
	}
}

root/app/model/FirstDb.php

<?php
namespace App\Model;

class FirstDb{
	public function db(){
		echo "Hi from first DB";
	}
}

root/index.php

<?php 
use App\Controller\Home;
use App\Model\FirstDb;

require_once __DIR__ . '/vendor/autoload.php';

$h = new Home();
$fdb = new FirstDb();

echo $h->index()."\n";
$fdb->db();

2. Without namespaces

root/composer.json

{
    "autoload": {
        "psr-4": {"": ["app/controller", "app/model"]}
    }
}

root/app/controller/Home.php

<?php

class Home{

	public function index(){
		echo "Hello from Home->Index!";
	}
}

root/app/model/FirstDb.php

<?php

class FirstDb{
	public function db(){
		echo "Hi from first DB";
	}
}

root/index.php

<?php 
require_once __DIR__ . '/vendor/autoload.php';

$h = new Home();
$fdb = new FirstDb();

echo $h->index();
$fdb->db();

3. Mix usage

root/composer.json

{
    "autoload": {
        "psr-4": {
            "": "app/"
        }
    }
}

root/app/controller/Home.php

<?php
namespace controller;

class Home{

	public function index(){
		echo "Hello from Home->Index!";
	}
}

root/app/model/FirstDb.php

<?php
namespace model;

class FirstDb{
	public function db(){
		echo "Hi from first DB";
	}
}

root/index.php

<?php 
require_once __DIR__ . '/vendor/autoload.php';

$h = new controller\Home();
$fdb = new model\FirstDb();

echo $h->index();
$fdb->db();

4. Composer addPsr4() without cnahge composer.json – dynamically add classes

root/composer.json

{
    "name": "some/test",
    "require": {}
}

root/app/controller/Home.php

<?php
namespace App\Controller;

class Home{

	public function index(){
		echo "Hello from Home->Index!";
	}
}

root/app/model/FirstDb.php

<?php
namespace App\Model;

class FirstDb{
	public function db(){
		echo "Hi from first DB";
	}
}

root/index.php

<?php 
use App\Controller\Home;
use App\Model\FirstDb;

$loader = require __DIR__ . '/vendor/autoload.php';
$loader->addPsr4('App\\', 'app/');

$h = new Home();
$fdb = new FirstDb();

echo $h->index()."\n";
$fdb->db();

5.Some important commands

Some important commands:

To initially install the defined dependencies:
php composer.phar update

to make sure the vendor directory is up in sync with your composer.lock file
php composer.phar install

To update to the latest versions
php composer.phar update

After adding the autoload field, you have to re-run this command:
php composer.phar dump-autoload

php composer.phar dump
calls composer dump-autoload.

how to create a composer.json by hand
php composer.phar init

To list all of the available packages
php composer.phar show

It will replace your composer.phar with the latest version:
php composer.phar self-update


all directories or files to search for classes:
{
    "autoload": {
        "classmap": ["src/", "lib/", "Something.php"]
    }
}
Wildcards (*) are also supported in a classmap paths, and expand to match any directory name:
{
    "autoload": {
        "classmap": ["src/addons/*/lib/", "3rd-party/*", "Something.php"]
    }
}


if your package includes PHP functions that cannot be autoloaded by PHP:
{
    "autoload": {
        "files": ["src/MyLibrary/functions.php"]
    }
}




HELP
=========================
to see the complete list of commands
composer list

--help combined with any of those can give you more information

Share and Enjoy !

Shares

Leave a Reply

Your email address will not be published. Required fields are marked *

*