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 ...
composer dump-autoload
after adding the namespace to composer.json? – Styphonvendor
folder anywhere in yourcomposer.json
file. That directory is created and populated by composer itself. Yourpsr-4
section should specify the path relative to your project's root. In your case, it would be similar to"Abkrim\\Rclub\\": "src/"
. See getcomposer.org/doc/04-schema.md#psr-4 for the official docs on PSR-4 autoloading. – Josh J