"Class HelloController not found" error on composer autoload config with psr4 - php

I try to create my own composer library. I've chose to use psr4 for autoloading mechanism. It works fine with the library project but something goes wrong when I add this library to another project as dependency. I expect the library project create an instance of a class which is located in main project. However this class cannot be found by composer autoloader.
My library project source is here : https://github.com/brnogz/kwinsey
My example project which uses this library like that(HelloWorld class is located in controller/HelloWorld.php file) : https://gist.github.com/brnogz/e27a1dd40ba00b818b23fe7ab8815fad

Please move all your sources to a src subfolder and use "src/" as the PSR-4 target folder. Autoloading from a project root folder is pretty much undefined behaviour.

Related

ZF3 - How to extends a controller from a framework in a vendor directory?

I'm working with ZendFramework3 for a project. I have a module that works
(for example : myProject/module/MyModule). Everything it's working but I would like to extends the controller which is in MyModule/src/Controller/CrudController. I mean, I have a simple module in my ZF3 project and I want the controller in this module to extends a Controller in a framework placed in myProject/vender/MyFramework/Mvc/Controller/
Problem is Fatal error: Class 'MyFramework\Mvc\Controller\ExtendedController' not found in C:\wamp\www\myProject\module\MyModule\src\Controller\MyController.php on line 7
<?php
namespace MyModule\Controller;
use MyFramework\Mvc\Controller\ExtendedController;
class MyController extends ExtendedController
{
}
I don't know what am I supposed to do to find the Controller placed in a framework in the vendor directory.
If someone could help me on this, it would be a pleasure.
Currently, I based my code on what Zend does. For example, we could find this
use Zend\Mvc\Controller\AbstractActionController;
I think the controller 'MyController' which extends the ExtendedController finds the right file but the controller is probably not load or something like that
Yours -)
EDIT : My problem has been resolved. The problem was the composer.json which needed to be updated.
I added this code in composer.json file :
"autoload" : {
"psr-0" : { "MyFramework\\": "vendor/MyFramework/{folder}/"},
},
It looks like your module is not found by composer. Have a look at the composer.json file from zend-mvc, especially the autoload section. That's how composer knows how to register packages.
AFAIK you can't just place a package in the vendor dir. You need to create a complete composer package and add it to your composer.json require section.
What might be easier during development is placing your module inside the ./module/ dir. You can than edit your module and when it's ready you publish it via packagist or a private composer repository.
To get this working you need to add MyFramework to the autoload section of your project:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
"MyFramework\\": "module/MyFramework/src/"
}
},
Obviously, you need to update the autoloader with composer dump-autoload after changing the autoload section.

Zend Framework 2 load class PHPGangsta_GoogleAuthenticator

I'm trying to load a class that has an underscore in it in ZF2.
This is the project I want to use: https://github.com/PHPGangsta/GoogleAuthenticator
The folder paths look like this:
/module
/Application
/Service
/MyService.php
/vendor/
/PHPGangsta
/GoogleAuthenticator.php
GoogleAuthenticator.php has a class named PHPGangsta_GoogleAuthenticator which I want to use in MyService.php, without having to require any files.
Also, I cannot change files inside PHPGangsta, because the project is submoduled under git.
Can you help configure zf2 class autoload?
Assuming you installed ZF2 using Composer (which is the recommended method), edit your composer.json to add phpgangsta/googleauthenticator to the require section. Then run composer install. That's it. You should then be able to use the library in your application - you don't need to do any additional autoload configuration.

Fatal error: Class 'CDbTestCase' not found

I'm new in php and I have to do the testing for an app. I'm trying to make unit testing but an error messages is displayed. I have many weeks ago and I cannot fix it, please help me!!
The message says: Fatal error: Class 'CDbTestCase' not found.
I read and follow many tutorials about this issue but it doesn't work.
I'm using Yii, Eclipse IDE and Composer.
I think the problem is in bootstrap.php but I don't use it because I'm working with composer, this is the composer.json
{
"require-dev": {
"yiisoft/yii": "1.1.*",
"phpunit/phpunit": "4.6.*",
"phpunit/phpunit-selenium": ">=1.2",
"codeception/codeception":"*",
"phpunit/dbunit": ">=1.2"
},
"autoload": {
"psr-0": {"": "vendor/autoload.php"},
"psr-4": {"": "/../framework/test/CDbTestCase.php"}
}
}
The bootstrap file is needed to load the yii framework. CDbTestCase is part of the yii framework so failing to include yii will give you this error if your tests depend on yii's unit test related classes.
Use the included bootstrap file and make sure you also include composer's autoload.php file. I normally add this to my yii config file (I believe by default, yii uses the test.php config file for custom testing related settings. You can include autoload.php inside this file)
Somewhere at the top of your yii config file
// Include composer autoload
require_once 'path/to/composer/vendor/autoload.php';
Your Composer autoloading is completely wrong.
Don't include the composer autoloader "vendor/autoload.php" in the autoloading of your own composer.json. There is only one autoloader, and it is created using your autoload data as well as any libraries.
The PSR-4 autoloading is also wrong. You have to state a directory where classes are put into files according to PSR-4. You point to one single file.
You don't use class prefixes for PSR-0 and PSR-4. This is bad for performance, because you are stating that ANY class could be in the directory. So Composer has to search for that class in this directory as well, even for classes that are guaranteed to not be there. Always use a prefix, and make it as long as possible to allow unique matches: One prefix should only ever point to exactly one directory structure.
You should explain why you don't use a bootstrap file in your tests. The minimal required bootstrap code for PHPUnit is to include the Composer autoloader. Additionally, you should add things that are required in your tests and have to be done once, like initialization of framework components -YMMV.

Own project via composer

I've got some libraries loaded through composer, and I'm wondering if it's possible to add my own library in the /vendor map, and then to have the composer autoloader load it? The structure would be something like /vendor/mylibrary/ and then a namespace mylibrary.
Would this be possible? Also would it be possible to add a different map to the composer autoloader? Like for example /app/src/ and then to have it load all the classes in that folder? Or do I have to make my own loader for that?
Thanks
Reading the composer documentation:
You can even add your own code to the autoloader by adding an autoload field to composer.json.
{
"autoload": {
"psr-0": {"Acme": "src/"}
}
}
Composer will register a PSR-0 autoloader for the Acme namespace.
You define a mapping from namespaces to directories. The src directory would be in your project root, on the same level as vendor directory is. An example filename would be src/Acme/Foo.php containing an Acme\Foo class.
After adding the autoload field, you have to re-run install to
re-generate the vendor/autoload.php file.
So basically, you just need to follow PSR-0, and tell composer where to find your library, by adding that line to your composer.json
Yes.You can achieve it. Configure your composer.json file as following:
{
"autoload": {
"classmap": [ "classes" ]
}
Here classes is the name of the directory where you have all your application related classes.Vendor related class should be auto detected as well. Just add the following line to achieve both at the same time:
require 'vendor/autoload.php';
And you can use the namesapce to reference your class like the following:
use classes\Model\Article;
Yes, of course it is possible to add own libraries and you should feel highly encouraged to do so. If your library is available publicly, you can simply register it at packagist.org. If not, it's a bit more complicated, but not impossible.
If your project does not follow the PSR-0 standard, composer will create a classmap for you. A custom autoloader is not supported.
I'd recommend you to read the (really excellent) documentation about all this and come back, if you're running into problems.
http://getcomposer.org/doc/

Composer package autoloading

I can't get my library to work. The directory structure is lib-name/src and inside theres a main dir and a tests dir, how do I tell composer to load from the /lib-name/src/main folder?
Link to my library github https://github.com/gerardorn/catalogo
There are 3 ways to map your classes for autloading with composer.
PSR-0
The recommended way is PSR-0 compatible. This protocol describes the directory structure of your library. Each namespace need to be a directory. Classes with underscores are separated as well (PEAR style).
In your case, the class Catalogable has a namespace gerardorn\catalogo. To be PSR-0 compatible the directory structure is:
- src
- main
- gerardorn
- catalogo
- Catalogable.php
In your composer.json you should put the following:
"autoload" : {
"psr-0" : {"gerardorn" : "src/main"}
}
Classmap
Alternatively you could use classmap. Classes are searched for in the directory regardless of the namespace.
"autoload": {
"classmap": ["src/main"]
}
Files
The 3th method files doesn't apply in your case.
PHPUnit
Note that PHPUnit is needed to test your library, but not to run your library. Therefor you shouldn't put that up as a required library.
It's good that you're writing unit tests. You should sign up for Travis CI. It will run your PHPUnit tests each time that you push to GitHub and warn you (by e-mail) if something got broken.
Your directory structure should be PSR-0 compatible.
i.e. your file Catalogable.php has a namespace gerardorn\catalogo, so your directory structure for this has to match the following.
- src
- main
- gerardorn
- catalogo

Categories