Understanding the composer PSR-4 without any frameworks

I am trying to understand and find out how this PSR-4 works, because I like refactoring with this standard, a small application.

According to several guides and a stack overflow message, my structure and files are fine.

/vendor
  /abkrim
     /rclub
        /src/
            ResellerApi.php

Content ResellerApi.php:

<?php

namespace Abkrim\Rclub;
// Also try namespace Abkrim\Rclub\ResellerApi;

class ResellerApi
{
   private $url;
   private $proxy;
   private $auth_userid;
   private $api_key;

  function __construct() {
      $this->url          = 'https://test.XXXXXX.com/api/domains/';
      $this->proxy        = '94.xxx.xxx.xxx:1111';
      $this->$auth_userid = '837465';
      $this->$api_key     = 'VU5EksjwGa28mA93tgviQd7eWgiSQLOz';
   }
   public function show() {
       //composer-autoload
   }
}

On my composer.json (global)

{
  "name": "abkrim/resellerclub",
  "description": "Una pequeΓ±a app para modificar los contactos .ES",
  "license": "MIT",
  "authors": [
    {
      "name": "Abdelkarim Mateos",
      "email": "abdelkarim@tamainut.com"
    }
  ],
  "autoload": {
    "psr-4": {
      "Abkrim\\Rclub": "vendor/abkrim/rclub/" //Final edition Work Fine 
      //"Abkrim\\Rclub": "src/"
    }
  }
}

And at my work dir / reseller.php

<?php

use Abkrim\Rclub\ResellerApi;

require_once __DIR__.'/vendor/autoload.php';  // That it he question. If not put autoload, not work.

// Also try below with up comment
//  use Abkrim\Rclub;

$con = new ResellerApi();
$con->show();

PHPstorm does not show an error.

But php show error ...

PHP Fatal error:  Class 'Abkrim\Rclub\ResellerApi' not found in /Volumes/iDatos/abkrim/ownCloudTDC/tamainut/desarrollo/resellerclub/reseller/reseller.php on line 7

Edited: After running the dump-autoload linker (or composer update) in the vendor / composer / autoload_psr4.php file

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Abkrim\\Rclub' => array($baseDir . '/src'),
);

Too many tutorials show examples, but this is mixed with Laravel, Sympony, ... I like to understand PSR-4 ...

+4
source share
1

, , . /vendor/abkrim/rclub/src/ResellerApi.php, , Abkrim\Rclub vendor/abkrim/rclub/, , Abkrim\Rclub\ResellerApi, , ResellerApi /vendor/abkrim/rclub/ResellerApi.php, . ? src.

composer.json :

"autoload": {
    "psr-4": {
        "Abkrim\\Rclub": "vendor/abkrim/rclub/src/"
    }   
}

composer dump-autoload, .

ResellerApi.php reseller.php, Abkrim\Rclub\src.

+4

Source: https://habr.com/ru/post/1616101/


All Articles