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/"}
}
}
Related
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();
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/"
}
}
Im trying to run Symfony Crawler in my script, but when I try to do that, I get 500 server error. Where is the problem? The code:
use Symfony\Component\DomCrawler\Crawler;
class Yt_downloader
{
private $__parser;
private $__uri;
public function __construct($cfg)
{
$this->__parser = new Crawler();
if ( is_array($cfg) ) {
foreach ( $cfg as $key => $value ) {
$this->{$key} = $value;
}
}
}
public function test()
{
print_r($this->__uri);
}
}
and the action:
require_once APPPATH . 'libraries/Yt_downloader.php';
$downloader = new Yt_downloader(array(
'__uri' => 'https://www.youtube.com/watch?v=...'
));
btw, my composer.json:
{
"name": "project",
"description": "",
"license": "MIT",
"require-dev": {
"symfony/css-selector": "~2.8|~3.0"
},
"suggest": {
"symfony/css-selector": ""
},
"autoload": {
"psr-4": { "Symfony\\Component\\DomCrawler\\": "" }
},
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
}
}
}
this is being used in Codeigniter project. I thought it can be composer problem, but when I try to use different library it works. I think there is a problem with namespaces or something. Maybe I can see log anywhere? I use ubuntu..
You need to require
"symfony/dom-crawler": "~2.8|~3.0"
In your composer.json file so that the crawler component is installed
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/"
}
},
I'm trying to figure out how to reference a custom class using composer
my composer.json file looks like this:
{
"name": "adtools_api",
"repositories": [
{
"type": "package",
"package": {
"name": "qz/adtools_middleware",
"version": "dev-master",
"source": {
"url": "repo-name",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"slim/slim": "2.*",
"qz/adtools_middleware": "src/"
}
}
and the folder structure looks like this:
app
routes
vendor
composer
qz
adtools_middleware
src
hello-world.php
slim
composer.json
index.php
I'm trying to reference the hello-world.php file which looks like this:
<?php
namespace HelloWorld;
class SayHello
{
public static function world()
{
return 'Hello World, Composer!';
}
}
?>
In the index.php file I'm trying to reference the class like this:
$hello = new HelloWorld\SayHello();
but getting an error telling me "Fatal error: Class 'HelloWorld\SayHello' not found in..."
If anyone can point me in the right direction that would be great! Thank you!
Can you check the autoload inside the vendor folder and see if your HelloWorld namespace is loaded?
If not, you may need to add autoload attribute to your composer.json file, like this
{
"autoload": {
"psr-0": {"HelloWorld": "qz/adtools_middleware/src/"}
},
to load the HelloWorld namespace in your project