Class LaravelTournamentSeeder does not exist with package developpement - php

I have coded a package, and I have a problem with namespace I thinka
My plugin organisation is:
- database
-- factories
-- migrations
-- seeds
- resources
- src
-- MyServiceProvider
-- MyController
In composer.json, I have:
"autoload": {
"psr-4": {
"Xoco70\\LaravelTournaments\\": "src"
},
"classmap": [
"src/"
]
},
So basically, all database folder has no namespace.
When I want to call
php artisan db:seed --class=LaravelTournamentSeeder
I get:
[ReflectionException]
Class LaravelTournamentSeeder does not exist
But LaravelTournamentSeeder exists : first it exists in my plugin, then, it exists in my laravel installation, because I published the assets.
Any ideas???

It is a composer autoloading problem.
Just run command composer dumpautoload in your console from the project root, you should be able to run it.

Related

How do I change the namespace of my application in Laravel?

I can't change the namespace of my application in Laravel 5.8.
I'm using artisan to change it:
php artisan app:name TestApp
Result is: There are no commands defined in the "app" namespace.
Looking at php artisan you should have a php artisan app:name NewNamespace
command to change the namespace. Make sure that you are on the latest laravel version.
Old answer
To change the namespace of your app you have to edit the composer.json file:
"autoload": {
"psr-4": {
"CustomNamespace\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
},
but you also have to edit each file in the app subfolders and in the various configurations files (for example in config/app.php, config/auth.php, etc).
After you have done all of that you can run: composer dump-autoload
Keep in mind that this is an error prone method, because if you forget to replace a namespace in any of your files anything can stop working as expected.
Another option would be to create a custom package with the name you want and register it with a custom namespace. For example:
Create a lib/yourpackage/src folder in the root of laravel installation
Edit the composer.json file to load your custom code:
"autoload": {
"psr-4": {
"App\\": "app/",
"CustomNamespace\\": "lib/yourpackage/src/",
},
"classmap": [
"database/seeds",
"database/factories"
]
},
Run composer dump-autoload as before
You can now use anywhere your files in your project by referring to the custom namespace you have choosen.
laravel change this command to app:namespace
you have to do this
php artisan app:namespace TestApp
Are you trying to change the namespace of your application or just the name ?
What do you want to change exactly in the namespace ?
EDIT
I think you must have touched something in your application because the command php artisan app:name AppName should work, I just tested it.
Have you ever tried before to change the namespace by yourself?
Otherwise, try composer dump-autoload before to make sure your autoloading is up to date.
This feature has been removed as of Laravel 6. It's not recommended to change the namespace.
However if you want to do it, you can add the command to your Console/Commands directory
https://gist.github.com/isluewell/b824c0aef32f5007170fcd0d8498b657

TestCase in laravel package not found

I'm converting my open source app as a package and it always throws error
PHP Fatal error: Class 'Laracommerce\Tests\TestCase' not found in .... on line 18
Based on other comments on every search I made, I just need to define it in my package composer.json's autoload-dev the location of my tests but still getting the error.
You are calling Laracommerce\Tests namespace but into the composer file you declared Laracommerce\Core\Tests
you need to:
a. change classes namespaces Laracommerce\Core\... to Laracommerce\...
b. or simply modify your composer file like this
"autoload":{
"psr-4": {
"Laracommerce\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Laracommerce\\Tests\\": "tests/"
}
},
Then you need to run composer dump-autoload
Try running a composer install, sometimes I get the same error but it gets fixed after that.

Class file not found by composer and psr-4

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

Class not found after auto loading in Laravel

I am struggling with PHP Namespaces and Auto loading in Laravel 5.4. I actually decided to put the models that I need to use in a definite and named folder as app/Models:
php artisan make:model Models/Customer
I have set Customer model's namespace to namespace Models;
Then, I created a route as follows:
Route::get('customer', function () {
$customer = \App\Models\Cutomer::find(1);
echo $customer;
});
To do auto loading work I opened the composer.json file located in the very root folder of the Laravel project and made the autoload to be as follows:
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"App\\": "app/",
"App\\Models\\": "App/Models"
}
},
After all of this I checked if my composer is an update version and ran dump-autoload:
composer self-update
composer dump-autoload -o
There is also worth of notice the contents of vendor/composer/autoload_classmap.php:
'App\\Models\\Customer' => $baseDir . '/app/Models/Customer.php',
'App\\Models\\Test\\Test' => $baseDir . '/app/Models/Test.php',
My problem is that whenever I execute the url: http://127.0.0.1:8000/customer I will encounter the following error output:
(1/1) FatalThrowableError
Class 'App\Models\Cutomer' not found
Would you please help me understand where of my work has been incorrect, and how to fix it? Thank you very much in advance.
The namespace in the model should be namespace App\Model; (hence why you call the class as \App\Models\Cutomer)
When you use php artisan make:model Models/Customer it should have set the namespace correctly then.
Also, you don't need to edit your composer.json to do this. Remove the additions you've made to your composer.json file for the autoloading and then run composer dumpautoload from the command line.
Hope this helps!
First, you have an error in your PSR-4 setup.
"App\\Models\\": "App/Models"
This does not map to a valid directory. Secondly, and more importantly, there is no need to autoload a nested namespace of one that is already declared. By autoloading App you will already autoload any nested namespaces as long as they conform to PSR-4 standards i.e namespace as directory name and filename as class name.
Composer setup only needs to be the following:
"psr-4": {
"App\\": "app/"
}

Symfony3 ClassNotFoundException after bundle creation

I wanted to start a new 3.3 project in Symfony and started as usual:
1.) Creating the new project: symfony new ArtProject
2.) Creating a new Bundle: php app/console generate:bundle (Paul/ArtBundle, yml, src/)
Then I run the local server and when I open 127.0.0.1:8000 I get this beautiful message:
(1/1) ClassNotFoundException
Attempted to load class "PaulArtBundle" from namespace
"Paul\ArtBundle". Did you forget a "use" statement for another
namespace? in AppKernel.php (line 19)
Which is strange and I haven't figured out why this happen so far. Before creating the Bundle, there was no error; I saw the typical startpage of symfony.
public function registerBundles()
{
$bundles = [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
......
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new AppBundle\AppBundle(),
new Paul\ArtBundle\PaulArtBundle(),
];
}
<?php
namespace Paul\ArtBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class PaulArtBundle extends Bundle
{
}
Any idea whats going on there? I did not change a thing, I only ran these commands.
I just installed a fresh copy of S3.3.4 (latest version as of this writing) using:
composer create-project symfony/framework-standard-edition s334 "3.3.4"
bin/console generate:bundle
Share across multiple apps: yes
namespace: Paul\ArtBundle
bundle name: PaulArtBundle
Target Directory: src/
Refreshed the browser and sure enough I got the class not found message.
The generate:bundle command is not updating the autload section of composer.json when a new namespace is introduced. Edit composer.json and:
# composer.json
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"Paul\\": "src/Paul"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
Then run
composer dumpautoload
And restart the server(maybe). That should fix the bundle class not found problem.
I have not used the generate:bundle command is quite some time ever since the standard was to put everything under AppBundle so I don't know how long this has been "broken". But at least three questions in the last week or so indicates it was something recent.
And by the way, when I refreshed the browser I got "Hello World" which threw me for a bit. Turns out the new bundle overrides the / route which is also sort of special.
And in case anybody is wondering why this started happening, Symfony 3.2 changed from
#composer.json
"psr-4": { "": "src/" },
To
"psr-4": { "AppBundle\\": "src/AppBundle" },
You could always just change it back though I think spelling out individual namespaces might be "better". Not sure.
And here is an issue with more details: https://github.com/symfony/symfony-standard/issues/1098
Looks like the maintainer favored a tiny speed improvement over breaking an existing command. Oh well. Symfony Flex is supposed to make everything great again.
If you generate a bundle for usage in multiple projects (with own namespace) you need to add it in the composer.json as follwed:
Lets assume your bundle name is CompanyFooBundle with namespace Company\Bundle\FooBundle then the composer autoload section should look like:
...
"autoload": {
"psr-4": {
"Company\\Bundle\\FooBundle\\": "src/Company/Bundle/FooBundle"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
},
...
This works for me in:
Generate your Bundle with
./console generate:bundle
And follow the steps as always, now, do what you want in your composer.json file with the line
"AppBundle\\": "src/AppBundle"
Replace with "": "src/" or add your bundle, for example: "BackendBundle\\": "src/BackendBundle"
Here's the new part:
Install composer in your bin directory, copy and paste the steps from https://getcomposer.org/download/
Up a level in your project directory, and in your root folder (of your project) run the next command
php ./bin/composer.phar update
Removing vendor dir and again running composer install helped me with same problem.

Categories