Composer autoloading with PSR4 - php

Probably something trivial but I have a problem with basic autoloading. I wanna create sandbox project just for testing new solutions so I've created following structure:
Sandbox
|- index.php
|- composer.json
|- vendor
| |- {autogenerated content}
|- src
|- Working.php
File composer.json looks like this:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar#example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"psr-4": {
"Sandbox\\": "src/"
}
}
Of course I've run composer.update after changes. Then I wrote a trivial body of Working.php:
<?php
namespace Sandbox;
class Working
{
public function __construct() {
echo "Hello World";
}
}
And of course index.php as well:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Sandbox\Working;
new Working();
?>
I checked permissions to those files just to be sure but when I try to run I get
PHP Fatal error: Class 'Sandbox\Working' not found in /var/www/Sandbox/index.php on line 6
I realize it's probably something trivial but what can be wrong here?

At your composer.json you are missing autoload key.
It should be like
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}

I believe
"psr-4": {
"Sandbox\\": "src/"
}
Should be:
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
So your file would be:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar#example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}

Related

Fatal error when instanciating Pusher - Can't find class

I have a project to test and play around, with the following structure:
app/
controllers/
HomeController.php
handlers/
models/
vendor/
composer/
psr/
pusher/
pusher-php-server/
src/
Pusher.php
PusherException.php
PusherInstance.php
tests/
composer.json
autoload.php
index.php
I tried to require the Pusher autoloader in my index file:
require 'vendor/autoload.php';
Which is the following:
// autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInite16b90ab01042d2a69b1d54243c9e23a::getLoader();
Now, in my HomeController.php, I have the following code:
namespace App\controllers;
use \App\handlers\Views as View;
use \App\models\Home_model as Home;
use \App\controllers\SessionController;
use \Pusher\Pusher as Pusher;
class HomeController {
private $pusher;
public function __construct() {
$options = array(
'cluster' => 'hmm',
'encrypted' => true
);
$this->pusher = new Pusher(
'secret',
'secret',
'secret',
$options
);
}
public function index() {
$data['message'] = 'hello world';
$this->pusher->trigger('my-channel', 'my-event', $data);
return $this->view->render('views/home/index.php');
}
}
But this returns me an error:
Fatal error: Class 'Pusher\Pusher' not found in
And I'm not sure what I'm doing wrong. Could someone explain me what I'm doing wrong?
In composer.json I get the following:
{
"name": "pusher/pusher-php-server",
"description" : "Library for interacting with the Pusher REST API",
"keywords": ["php-pusher-server", "pusher", "rest", "realtime", "real-time", "real time", "messaging", "push", "trigger", "publish", "events"],
"license": "MIT",
"require": {
"php": "^5.4 || ^7.0",
"ext-curl": "*",
"psr/log": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.7"
},
"autoload": {
"psr-4": {
"Pusher\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "": "tests/" }
},
"config": {
"preferred-install": "dist"
},
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
On Github, they mention that the library depends on cURL and JSON modules.
Not realy sure if this has something to do with my issue?
I'm still stuck, so any help is greatly appreciated.
Also, I'm using a .htaccess file, to rewrite my urls.
I have managed your code to work with right composer.json next to index.php:
{
"name": "awesome/project",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Author",
"email": "author#gmail.com"
}
],
"autoload": {
"psr-4": {
"": ""
}
},
"require": {
"pusher/pusher-php-server": "dev-master"
}
}
Then just run composer install.
My index.php contents:
<?php
require 'vendor/autoload.php';
use app\controllers\HomeController;
$ctrl = new HomeController();

Composer autoload can't find class

I'm trying to create a MVC structure and use composer to autoload everything.
But I keep getting this error:
Fatal error: Uncaught Error: Class 'App\Init' not found in C:\wamp64\www\activity\Public\index.php on line 5
|MainFolder
|App
|Public
|Vendor
|ACT
|composer
|autoload.php
|composer.json
composer.json:
{
"name": "vendor/activity",
"description": "descrip",
"require": {
"php": ">=5.6.25"
},
"authors":[
{
"name": "John Doe",
"email": "johndoe#gmail.com"
}
],
"autoload":{
"psr-4": {
"ACT\\": "vendor/",
"App\\": "/"
}
},
"config":{
"bin-dir": "bin"
}
}
App\init.php
<?php
namespace App;
class Init
{
public function __construct()
{
echo "Loaded!!";
}
}
Public\index.php
<?php
require_once '../vendor/autoload.php';
$init = new \App\Init;
\Vendor\composer\autoload_namespaces.php
<?php
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
"ACT" => array($vendorDir . false),
"App" => array($baseDir . '/'),
);
Obs: Already did composer dump-autoload
Don't manually put things in /vendor.
While adhering to #1, don't reference /vendor in autoload, the packages should all have their own fully-functionaly autoloaders that composer will find and use.
You need to specify more of the path in your autoload.
"autoload":{
"psr-4": {
"App\\": "App/"
}
},
Think of it like telling composer "look for things starting with the namespace foo\bar\ in the following folder".
Note: The folder name doesn't have to match the namespace.
Eg: Following the suggested Vendor\Package\ scheme for PSR/Composer
{
"autoload": {
"psr-4": {
"sammitch\\meatstacker\\": "src/"
}
}
}
And then:
\sammitch\meatstacker\Client maps to src/Client.php
\sammitch\meatstacker\Bread\Rye maps to src/Bread/Rye.php
and so on

PSR4 not working

I have setup a folder structure like this for a package of legacy classes
vendorname/legacy/src/ClassA.php
namespace Vendorname\Legacy;
class ClassA{}
vendorname/legacy/src/Folder/Class2.php
namespace Vendorname\Legacy\Folder;
class FolderClass2{}
With composer I'm loading this from a github repo like this:
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:username/vendorname-legacy-classes.git"
}
],
"require": {
"vendorname/legacy": "master#dev"
}
When I load ClassA like this it works:
use Vendorname\Legacy\ClassA;
$a = new ClassA();
However none of my subfolder'd classes work:
use Vendorname\Legacy\Folder\FolderClassB;
$b = new FolderClassB();
Class 'Vendorname\\Legacy\\Folder\\FolderClassB' not found
I have already defined the source folder with a file vendor\vendorname\composer.json
{
"name": "vendorname/legacy",
"description": "Vendorname Legacy classes",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"Vendorname\\Legacy\\": "src"
}
},
"extra": {
"branch-alias": {
"master": "master"
}
}
}
you need to define one thing more to your composer.json
{
"autoload": {
"psr-4": {"Vendorname\\Legacy\\": "vendorname/legacy/src/"}
}
}

How to use my Package in Laravel5

I have a question related to Package for Laravel 5. I am creating one package and tried to use that. package successfully created and using composer i can also get that in New Laravel setup , issue is that when i tried to use that it's says class not found. Here's my composer.json and Steps that i followed:
for e.g. my username = git_test and packagename = mypackage
My Package Structure :
**git_test > mypackage > src
My composer.json file
{
"name": "git_test/mypackage",
"description": "XXXXXXXXXX",
"keywords": ["laravel"],
"license": "MIT",
"authors": [
{
"name": "XXXXXXX",
"email": "XXXXXX#gmail.com"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*"
},
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},
"minimum-stability": "dev"
}
Here's my src/myclass.php
namespace git_test\mypackage;
class myclass {
function test(){ echo "This is Test"; }
}
Now i am going to use this in my new laravel project so i add package in my directory composer and try to use the myclass in my HomeController
HomeController Code
use git_test\mypackage\myclass as TaskClass;
class HomeController extends Controller {
public function index()
{
$atTaskObj = new TaskClass('');
}
I got the error like "git_test\mypackage\myclass" Not Found. where i am doing wrong? any suggestion please.
Thanks in Advance!!!
PSR-4 paths have to end with \\:
"autoload": {
"psr-4": {
"git_test\\mypackage\\": "src/"
}
},

Composer for autoloading php

Hi i have this folder structure:
I'm using composer for autoloading my files but it is dont work .. i do it first time and i dont know how to implement this.
My composer.json
{
"name": "Some name",
"description": "Some Framework",
"minimum-stability": "stable",
"license": "proprietary",
"authors": [
{
"name": "Some names of authors",
"email": "some#gmail.com"
}
],
"autoload": {
"psr-4": {
"Apison": "/../sdk/"
}
}
}
And my index.php
<?php
require_once 'vendor/autoload.php';
$app = new \Apison\Sdk\App();
When i update my composer it will write: Nothing to load and PHP will catch exeption on line with $app = new \Apison\Sdk\App();
Thanks for your tips
namespaces need \\:
"Apison\\": "../sdk"
documentation:
https://getcomposer.org/doc/04-schema.md#psr-4
Based on our chat, the solution is this:
"autoload": {
"psr-4": {
"Apison\\Sdk\\": "sdk"
}
}
Then the namespaces and file structure was changed to comply with the psr-4 standard

Categories