i have followed this tutorial http://www.youtube.com/watch?v=yAzd7Ig1Wgg to get namepsaces with psr-0 working in my laravel 4 application but no joy. What im i missing?
my folder sttructure
>App
>>|Acme
>>>>|Billing
>>>>>>>|Billing.php
My billing class
<?php namespace Acme\Billing;
class Billing {
function __construct()
{
dd('finally!!!');
}
}
My composer file
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/lib"
],
"psr-0":{
"Acme":"app/"
}
},
and my route
Route::get('/', function(){
new Acme\Biling\Billing;
});
I keep getting this error
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'Acme\Biling\Billing' not found
I found what i was missing.
"psr-0":{
"Acme":"App/Acme" ***missing piece***
}
and this works even with psr-4. hope it helps someone.
nb: follow folder structure and check casing when naming.
Try to run php artisan dump-autoload command.
Related
I have an app with laravel 7, and i upgrade composer 1.10 to composer 2.0, and i have this problem:
Illuminate\Contracts\Container\BindingResolutionException
Target class [DatabaseSeeder] does not exist.
at vendor/laravel/framework/src/Illuminate/Container/Container.php:807
803|
804| try {
805| $reflector = new ReflectionClass($concrete);
806| } catch (ReflectionException $e) {
> 807| throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
808| }
809|
810| // If the type is not instantiable, the developer is attempting to resolve
811| // an abstract type such as an Interface or Abstract Class and there is
+37 vendor frames
38 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
I tried many solution (even for Laravel 8 ...) without success.
Here is the autoload of the composer.json
"psr-4": {
"App\\": "app/",
"DatabaseSeeder\\": "database/seeds"
},
"files": [
"app/helpers.php"
]
}
Thx !
EDIT
Here is what I just found: When I add in the composer.json :
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories",
"Database\\Seeds\\": "database/seeds",
"DatabaseSeeder\\": "database/seeds/",
} },
and that I modify line 84 of SeedCommand.php 'src/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php', it work !!!
$class = $this->laravel->make($this->input->getOption('class'));
by
$class = $this->laravel->make('Database\\Seeds\\DatabaseSeeder');
But this is absolutely not maintainable...
So the problem is, there is a problem when DatabaseSeeder is set.
6An idea ?
I found the solution:
Laravel documention : https://laravel.com/docs/7.x/seeding#introduction
All seed classes are stored in the database/seeds directory.... By default, a DatabaseSeeder class is defined for you.
This folder is not compatible with the PSR-4, so you have to use the classmap in the composer.json with Laravel 7.
It is therefore also necessary to remove the namespaces in the files of seeds.
Here is my composer json:
"autoload": {
"psr-4": {
"App\\": "app/",
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
}
I solved it as follows:
I followed what the documentation said regarding Sedeers and factories
I ran composer dump-autoload
Update composer to version 2
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 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
When running the example code on the laravel docs php artisan make:request StoreBlogPostRequest to create a new validation controller, I get the following error
[RuntimeException]
Unable to detect application namespace.
I'm not sure what's wrong, I've done some searching, but nothing really explains this error. Any ideas?
In Laravel 5, an "application" is a collection of PHP files under a single namespace, stored in the folder app/
By default, and in most of the Laravel 5 sample code from the docs, this namespace is App\. For example, one controller in your application might look like this.
namespace App\Http\Controller;
class MyController
{
//...
}
When Laravel generates code (i.e. when you use the make:request command), it needs to know what this application namespace is (it's possible to change the namespace with the artisan app:name command). For some reason, in your system, Laravel 5 can't detect the namespace.
If you look at the section of Laravel 5 core code that detects the namespace
#File: vendor/laravel/framework/src/Illuminate/Console/AppNamespaceDetectorTrait.php
protected function getAppNamespace()
{
$composer = json_decode(file_get_contents(base_path().'/composer.json'), true);
foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path)
{
foreach ((array) $path as $pathChoice)
{
if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) return $namespace;
}
}
throw new RuntimeException("Unable to detect application namespace.");
}
You'll see that Laravel detects the namespace by looking at your composer.json file, and looking for thefirst valid psr-4 namespace.
My guess is your composer.json file is missing the namespace
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
Add that back in, and you'll be good to go.
Usually, this error can be mapped to syntax issues or errors in composer.json file. Check for any trailing commas or Auto load issue. For e.g.
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5",
},
This should be..
"require-dev": {
"barryvdh/laravel-debugbar": "^3.5",
"phpunit/phpunit": "^7.5"
},
See no trailing commas at the end of "phpunit/phpunit": "^7.5"