4

I try to understand and learn how it works PSR-4, because I like refactor with this standard, a little app.

According several manuals, and post on stack overflow, my structure and files are ok.

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

ResellerApi.php content:

<?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 on my work dir /reseller.php

<?php

use Abkrim\Rclub\ResellerApi;

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

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

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

PHPstorm not show 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 run composer dump-autoload (or composer update) on file vendor/composer/autoload_psr4.php

<?php

// autoload_psr4.php @generated by Composer

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

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

Too many manuals show examples, but it's a mixed with Laravel, Sympony, ... I like understand PSR-4 ...

abkrim
  • 3,512
  • 7
  • 43
  • 69
  • Have you run `composer dump-autoload` after adding the namespace to composer.json? – Styphon Nov 16 '15 at 17:29
  • You shouldn't be specifying the `vendor` folder anywhere in your `composer.json` file. That directory is created and populated by composer itself. Your `psr-4` section should specify the path relative to your project's root. In your case, it would be similar to `"Abkrim\\Rclub\\": "src/"`. See https://getcomposer.org/doc/04-schema.md#psr-4 for the official docs on PSR-4 autoloading. – Josh J Nov 16 '15 at 18:58
  • Not work. Create a branch on github woth new composer.json, and run composer dump-autoload. https://github.com/abkrim/rclub/tree/not-vendor – abkrim Nov 16 '15 at 19:07

1 Answers1

4

The problem is your file isn't where you told autoloader to look. Your file is in /vendor/abkrim/rclub/src/ResellerApi.php and you've told autoloader that the namespace Abkrim\Rclub is in vendor/abkrim/rclub/, so by specifying Abkrim\Rclub\ResellerApi you're telling autoloader that ResellerApi is in /vendor/abkrim/rclub/ResellerApi.php, see the problem? You missed out the src folder.

Try changing your composer.json file to:

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

Once you've made the change make sure to run composer dump-autoload to update autoloader.

Alternatively you can update your ResellerApi.php and reseller.php files to use Abkrim\Rclub\src.

Styphon
  • 10,304
  • 9
  • 52
  • 86
  • Thanks. But not work. Y put Abkrim\\Rclub": "vendor/abkrim/rclub/src/ and after run composer dump-autoload and composer update. On autoload_psr4.php line 'Abkrim\\Rclub' => array($vendorDir . '/abkrim/rclub/src'), and I run. I get same error. – abkrim Nov 16 '15 at 18:13
  • _The problem is you're file_. Sorry @abkrim, apparently you are a file :D – DeDee Nov 16 '15 at 18:37
  • 1
    I don't understand your comment. I'm a file? – abkrim Nov 16 '15 at 18:56
  • I put files on github https://github.com/abkrim/rclub.git (And I have not put myself ;-D ) – abkrim Nov 16 '15 at 18:57
  • 1
    @DeDee Haha, I was busy when I was answering, corrected that – Styphon Nov 16 '15 at 21:32
  • @abkrim I typed `you're file`, i.e. `you are file`. I meant your file. – Styphon Nov 16 '15 at 21:32
  • Thanks @Styphon. Your tip work fine after put inlcude autoload... require_once __DIR__.'/vendor/autoload.php'; A despite... – abkrim Nov 17 '15 at 09:57