I have create in vendor directory the following structure for my package:
/vendor/
koala/
bamboo-sdk/
src/
Engine2/
Utils/
I then run
$composer init
inside koala folder.
Lately I went to Laravel5 composer.json main. And modified the autoload.psr-4 part like this:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
...
"Koala\\BambooSdk\\": "vendor/koala/bamboo-sdk/src"
}
},
I then run
$composer dump-autoload
in order to have /vendor/composer/autoload_classmap.php updated with the new references.
But I dont see it changing.
Also having a new controller with directive:
use Koala\BambooSdk\MyClass;
and then below
$myobj = new MyClass();
will result in a error:
FatalErrorException in MyController.php line 165:
Class 'Koala\BambooSdk\MyClass' not found
Related
I've tried now some different approaches and read articles here and elsewhere but i can't figure out what i'm doing wrong.
I'm developing a package and I want it to be installed via symlink in a different project to develop both parts simultaneously without the need of updating the dependencies over and over again.
The thing I cant get to work is the autoloading.
Here is my setup:
Project composer.json
{
"repositories": [
{
"type": "path",
"url": "../../FormTableComponent",
"options": {
"symlink": true
}
}
],
"require": {
[...]
"bluechord/formtablecomponent": "#dev"
}
}
FormtableComponent Folder Structure
FormTableComponent/
src/
| Container.php
composer.json
FormtableComponent composer.json
{
"name": "bluechord/formtablecomponent",
"description": "...",
"autoload": {
"psr-4": {
"BlueChord\\FormTableComponent\\": "src/"
}
},
"require" : {
[...]
}
}
FormtableComponent Container.php
<?php
/**
* FormTable Container. The Container Class that registers all Parts of the
* Component.
*/
namespace BlueChord\FormTableComponent;
class Container {
function __construct() {
}
}
When I try to use the class and instantiate it I get
Uncaught Error: Class 'BlueChord\FormTableComponent\Container' not found
Thanks for your help!
ADDITIONAL INFO:
A simple Test.php which reproduces the error inside the project
<?php
require_once 'bin/vendor/autoload.php';
echo "BCOSP Test";
use DebugBar\StandardDebugBar;
use BlueChord\FormTableComponent\Container;
$debugbar = new StandardDebugBar(); --> WORKS
echo StandardDebugBar::class ."\n";
$container = new Container(); --> ERROR
echo Container::class . "\n";
Composer
The vendor/composer/autoload_psr4.php does NOT contain any array key for my package.
If I run composer dump-autoload inside my main project nothing changes.
If I run composer dump-autoload inside my package it creates the correct autoload_psr4.php
I am using Laravel 5.8 and I created a custom class named StatusLib.php in the app/library folder.
StatusLib.php
namespace App\library;
class StatusLib
{
CONST SUCCESS = '100';
CONST SUCCESSWITHMESSAGE = '101';
}
I can call this status .
StatusLib::SUCCESS
When I add this following use code in the controller.
use app\library\StatusLib;
How can I add this StatusLib class in autoload and access from anywhere in the project?
Namespaces are case-sensitive.
In your StatusLib class you have App\library;, however, in your controller you've used app\library -- these are not the same.
Change your use statement in your controller to be:
use App\library\StatusLib;
You may also need to run:
composer dumpautoload
Just FYI, Laravel comes with the app directory already set up for autoloading.
In your composer.json file, after the classmap array, add a psr-0:
"autoload" :{
"classmap": [
...
],
"psr-0": {
"library": "app/"
}
}
Run composer dump-autoload.
Hope it helps.
where do you want to use it?
it will be automatically autoloaded because app folder is loaded in composer.json
here:
"autoload": {
"psr-4": {
"App\\": "app/"
},
},
I'm creating a web app with Slim and Twig. The libraries I use work perfectly, I can call them easily with no problem. However my own classes are not found by composer.json autoload psr-4 (psr-0 doesn't find them either)
Here is my file system:
project
|composer.json
|src
|public
| |index.php
|classes
| |Application.php
| |middlewares
| |SecurityMiddleware.php
|templates
|TemplateController.php
|main
|MainController.php
Here is my composer.json:
{
"authors": [
{
"name": "Jean-Marc ZIMMER",
"email": "#################gmail.com",
"role": "Developer"
}
],
"require": {
"slim/slim": "^3.11",
"slim/extras": "*",
"twig/twig": "^2.5",
"slim/twig-view": "^2.4",
"slim/views": "^0.1.3"
},
"autoload": {
"psr-4": {
"src\\": "src",
"middlewares\\": "src/classes/middlewares",
"classes\\": "src/classes",
"templates\\": "src/templates"
}
}
}
Then src/classes/Application.php:
<?php
namespace classes;
class Application extends \Slim\App {
public function __construct($container = array()) {
parent::__construct($container);
}
}
And finally my index.php file:
<?php
require '../../vendor/autoload.php';
$app = new \classes\Application([
"settings" => [
"displayErrorDetails" => true
]
]);
$app->run();
When I run composer dump-autoload, the command outputs:
Generated autoload files containing 0 classes
then exits with status code 0. It should find 4 classes, right ?
And running the app shows the error:
Fatal error: Uncaught Error: Class 'classes\Application' not found in /opt/lampp/htdocs/project/src/public/index.php:5
I'm sure I'm missing something, indicating a namespace or something. Can anyone help me ?
Edits:
I tried using the --optimize or the --classmap-authoritative option for dump-autoload. Changed nothing.
Adding a '/' to the folder names in composer.json doesn't change anything.
I got a solution from another source. I don't personally like it, but it works.
The file system wasn't changed.
composer.json autoload:
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
src/public/index.php:
<?php
require '../../vendor/autoload.php';
$app = new \App\classes\Application([
"settings" => [
"displayErrorDetails" => true
]
]);
$app->run();
src/classes/Application.php:
<?php
namespace App\classes;
class Application extends \Slim\App {
public function __construct($container = array()) {
parent::__construct($container);
}
}
I'm going to work from this functional base and see if I can get the result I want. If I do, I'll edit this answer.
Ensure your composer.json references your deployment paths. For example:
Dockerfile
FROM php:7.2-apache
COPY src /var/www/html
COPY vendor /var/www/vendor
composer.json
{
"autoload": {
"psr-4": {
"Acme\\": "html/classes/"
}
}
}
i.e. html/classes/ not src/classes/
I have a folder named "Models" located at app/models in my laravel project directory.
I want to use all models by a single line of code in controller, instead of using each one like:
use App/Models/User;
use App/Models/Roles;
You could try this
spl_autoload_register(function($className) {
include_once $_SERVER['DOCUMENT_ROOT'] . '/app/models/' . $className . '.php';
});
You need to add this folder in your composer.json file.
"autoload": {
"classmap": [
"database/seeds",
"database/factories",
"app/Models"
]
}
then run composer update
I am using Laravel 5.3 and for testing purpose, I have created a folder called "Helpers" is "app" folder. In "Helpers" folder, there is a file named credentials.php where I will declare some strings then I will use it later. Credentials.php looks like
`
<?php
namespace App\Helpers;
class Credential
{
}
`
In the 'tests' folder there is folder called 'links' and in this folder there is file called something.php and it looks like
<?php
use App\Helpers\Credential;
class something extends TestCase
{
}
and its working correctly...Problem arising when I am trying to put the "helpers" folder from "app" to "tests" folder. I dont want to put the "Helper" folder in "app" directory. I have changed namespace and other things, nothing works. I am new to laravel and seeking your help and suggestion.
Update Composer.json and add files key in autoload section.
"autoload": {
"files": [
"tests/helpers.php"
],
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
}
},
After Adding this, run the following command.
composer dump-autoload