I just need to autoload some classes, and I don't like the psr-0 namespace insanity (no offense).
This used to work just fine in my project:
"psr-0": {
"": [
"app/controller/",
"app/model/"
]
}
For some reason it doesn't work anymore, even though I'm using the same Composer version. I need it for a new project that is also using Silex. Could this be a conflict with Silex?
I know about the "classmap" option, but it's kind of useless because it requires that I run "composer install" every time I add a new class.
Any ideas?
Try to use "primitive" JSON properties; not an array (like in your example).
This works for me with psr-4 like you say, with "": "app/":
{
"autoload": {
"psr-4": {
"Robbie\\": "core/",
"": "app/"
}
},
"require": {
"monolog/monolog": "1.2.*"
}
}
This gives me the Robbie namespace under the directory core, as an example of sources not controlled by composer, the 3rd party (vendor) Monolog namespace and my default or non-namespace for sources underneath the app directory.
After a composer update, all of them are available when including the generated autoload.php:
<?php
require_once 'vendor/autoload.php';
// ...
?>
Use classmap in instead of psr-4:
"autoload": {
"classmap": ["models/"]
}
If you just want to regenerate the autoload file use composer dump-autoload.
Related
I am having very hard times understanding how to use autoloading by psr-4.After loading vagrant and setting and testing all variables in Homestead.yaml I have prepared a file structure as the following:
\app
\folder
-- test.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
and the following are my codes:
index.php
<?PHP
namespace app;
require('vendor/autoload.php');
$object = new folder\test();
composer.json
"autoload":{
"psr-4":{
"app\\": "app"
}
}
test.php
<?php
namespace app\folder;
class test
{
function __construct ()
{
echo 'construction done right.';
}
}
But, after trying to visit the page, these are the error message displayed on the page:
(!) Fatal error: Uncaught Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
( ! ) Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
Would you help me understand and fix this error?
It doesn't work because you have told Composer that the classes from the app namespace are in the app subdirectory but there is no app subdirectory.
The entire application is stored in the app directory and it's name doesn't really matter for the application. The classes of the app namespace are stored in the current directory and the sub-namespaces are stored in subdirectories with the same name.
Accordingly, your composer.json file should read:
"autoload": {
"psr-4": {
"app\\": ""
}
}
Or, to be more clear, you can put . (the current directory) as the location of the app\ namespace:
"autoload": {
"psr-4": {
"app\\": "."
}
}
After you make the change run composer dump-autoloader in the main application directory and it will start working.
To fix it for your current setup, use the following:
"autoload":{
"psr-4":{
"app\\": ""
}
}
Your composer.json is in the app-directory, so there's no subdirectory named app to reference.
I would actually recommend to change your directory structure to the following:
\app
\src
\folder
-- test.php
-- index.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
And then in composer.json, set the following:
"autoload":{
"psr-4":{
"app\\": "src"
}
}
This makes sure that all files belonging to your 'app' namespace are contained within a single subdirectory.
Finally, I would recommend you to use a vendor namespace to prevent conflicts, and to use the naming guidelines from PSR-2.
I think you won't need PSR-4 just add classmap
classmap parameter for example :
"autoload": {
"classmap": [
"app"
]
}
After adding this run composer dump-autoload and you should see a number of classes being added.
Hope this helps.
in composer.json try to set
"autoload":{
"psr-4":{
"": "src/"
}
}
then do execute this command
composer dump-autoload -o
I've been testing getting my packages up to Packagist with a simple class I made. Whenever I require it in a different project, it says the class cannot be found. Is something wrong with my composer.json autoload block?
Here's my project repo file structure:
- src
- prodikl
- View.php
- .gitignore
- composer.json
And here's my composer.json:
{
"name":"prodikl/simple-view",
"description":"A simple to use, basic View object.",
"require" : {
"php" : ">=5.3.0"
},
"autoload": {
"psr-4": {"prodikl": "src/"}
}
}
And finally, in my View.php:
<?php
namespace prodikl;
class View
{
...
}
But whenever I require it into a project and do require "vendor/autoload.php" and use use prodikl\View; it it keeps saying not found
You just need to point your autoloader down one more directory:
"autoload": {
"psr-4": {"prodikl": "src/prodikl/"}
}
This means "classes that belong to the \prodikl namespace can be found in the src/prodikl/ directory."
You might need trailing backslashes on the namespace name too, not sure how picky Composer is about it:
"psr-4": {"prodikl\\": "src/prodikl/"}
EDIT | I basically would like to tell composer to populate
autoload_classmap.php with a mapping of all files/classes under the
directory "web/". This houses all the application specific classes -
some which follow PSR-0 others do not.
I have a composer.json:
{
"name": "company/project",
"description": "Internal management system",
"require": {
"swiftmailer/swiftmailer": "^5.4",
"slim/slim": "2.4.2"
},
"autoload": {
"psr-0": {
"Application_Ancillary_": "web/private/module/rpi/ancillary/"
}
}
}
This "autoload" will kind of work - but there are cases where the classnames do no map according to PSR-0 the classname might be something like
Application_TestSomething => web/private/module/test/ApplicationTestSomething
When I tried to use 1:1 classname => file mapping it didn't work when I ran:
composer dumpautoload -o
Also the "Slim" classes are being included in the autoload_classmap.php
Ideally all the composer included packages (ie: Slim, etc) would not be part of the classmap file as I have hundreds of legacy files which I would rather include.
I assume that up until now someone manually edited the autoload_classmap.php
Any suggestions?
In case you don't follow any standard (PSR-0/PSR-4), use classmap:
"autoload": {
"classmap": [ "web" ]
}
I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}
Is there a way to make Composer run an installer plugin located in ./src/?
The relevant docs are rather unclear as to how it should be done, assuming it's possible to begin with.
If not, the docs also seem to suggest that it's possible to install a plugin in COMPOSER_HOME, but say essentially nothing as to how this can be done — any pointers would be most welcome.
Just to clarify: the problem isn't about autoloading the class as much as it is making composer load the plugin when running composer install or composer update.
My project setup:
./app
…
./src
./src/Hello
./src/Hello/HelloPlugin.php
./vendor
…
The plugin file:
<?php # ./src/Hello/Hello.php
namespace Hello;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
class HelloPlugin implements PluginInterface
{
public function activate(Composer $composer, IOInterface $io);
{
echo "Hello World!".PHP_EOL;
}
} # END class
The relevant lines of the project's composer.json file:
{
"name": "hello/world",
"type": "project",
"autoload": {
"psr-0": {
"Hello\\": "src/"
}
},
"require": {
"composer/installers": "~1.0",
"composer-plugin-api": "~1.0"
},
"extra": {
"class": ["Hello\\HelloPlugin"]
}
}
The composer autoload is used to load all the namespaces of packages you require in you project and it is not intended to load plugins during the composer install or composer update hook.
The only way to load your own plugin class, is to implement your own composer plugin, a normal package of type composer-plugin, which will be used by composer to load your HelloPlugin class. Otherwise, your plugin class will not be loaded during the composer install hook.
I have built a composer-plugin myself, here you can find it https://github.com/mnsami/composer-custom-directory-installer , for references.
And if you have more questions/or help, please don't hesitate to ask.
"autoload": {
"psr-0": {
"Hello\\": "src/"
}
}
https://getcomposer.org/doc/01-basic-usage.md#autoloading