Laravel phpspec cannot be autoloaded - php

I am trying to setup phpspec in a Laravel project.
I have installed phpspec properly and am running phpspec inside my project without any trouble.
My composer.json file has the following relevant lines:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"Acme\\": "app/Acme"
}
},
My phpspec.yml:
suites:
app_suite:
namespace: Acme
src_path: app
spec_path: app
spec_prefix: spec
Executing phpspec describe Acme/Foo created app\spec\Acme\FooSpec.php file.
Executing phpspec run created app\Acme\Foo.php
But I am getting the following error when I run "phpspec run" -
The type Acme\Foo was generated but could not be loaded. Do you need
to configure an autoloader?
Everything is fine, except for the auto-loading error. How can I solve the issue?

try
composer dump-autoload
It just regenerates the list of all classes that need to be included, when you have a new class inside your project

Related

Getting error There are no commands defined in the "module" namespace with nwidart

I'm trying to get started with nwidarts modular laravel architecture. I just got done installing it according to it's site: https://nwidart.com/laravel-modules/v2/basic-usage/creating-a-module
Now when I run the command php artisan module:make <module-name> I get the error:
[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "module" namespace.
I am running this in the root folder of my laravel app. So I am not entirely sure I installed it in the right place but there is an nwidart folder and stuff in my apps vendor folder so I guess I did it right? What did I do wrong?
follow the instructions on command
step 1: composer require nwidart/laravel-modules
step 2: php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
step 3: insert in composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
step 4: composer dump-autoload

Artisan not able to detect application namespace

Very recently, my application stopped working properly for some reason and I had to change my application namespace, so it is projectname\Model instead of App\Model.
Now after this change, everything started working normally, except php artisan commands.
When I call php artisan, I can have the lists of artisan commands but none works, I always get [RuntimeException] Unable to detect application namespace..
For exemple, I tried running php artisan make:controller ShoppingCartController and I get this error.
I looked online and a lot of people say it's a problem with the composer.json, but I tried composer diagnose and nothing stands out, and I updated composer and also tried composer dump-autoload.
And in my composer.json, I have this part I changed recently:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"projectName\\": "App/"
}
},
And I carefully looked to be sure there was no extra comma in the .json.
I'd like to still be able to use php artisan commands so I'd like to solve this.
Thanks,
When you rename your default namespace, the second parameter should be the name of the 'app' folder. so instead of
"projectName\\": "App/"
You should have
"projectName\\": "app/"

Create Module in laravel 5.3

I want to make a modulare laravel 5.3 application, I try to use nwidart/laravel-modules laravel package from composer, I follow this tutorial laravel modules package and it work, I have my Blog module created and the BlogServiceProvider class too.
But when I do php artisan module:use Blog I get this error in composer [Symfony\Component\Debug\Exception\FatalErrorException]
Class 'Modules\Vente\Providers\BlogServiceProvider' not found
here is exact solution for this
By default controllers, entities or repositories are not loaded
automatically.
You can autoload your modules using psr-4. For example :
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
},
1) copy the above code and paste it in your Composer.json
2) then do composer dumpautoload
3) then do php artisan module:use blog

Composer require-dev requireing dependencies in different packages require-dev

I have some tests that are namespaced being autoloaded in package A using
"autoload-dev": {
"psr-4": {
"Vendor\\PackageA\\PhpUnit\\": "tests/PhpUnit"
}
},
This works fine.
I have another package, package B which also have namespaced tests that require one of the namespaced tests in package A
"autoload-dev": {
"psr-4": {
"Vendor\\PackageB\\PhpUnit\\": "tests/PhpUnit"
}
},
However, when I try and include the file in Package B, the class is not found
use Vendor\PackageA\PhpUnit\MyTestFromA;
class MyTestFromB extends MyTestFromA
{
Making me think that the autoload-dev stuff from other packages is not being loaded.
PHP Fatal error: Class 'Vendor\PackageA\PhpUnit\MyTestFromA' not found in /full/path/to/PackageBClass.php on line 3
When I try and import a file that is being autoloaded using autoload from package B rather than autoload-dev, I get no error.
How can I overcome this?
Part of me is thinking to make a package just for the tests and have it autoloaded in both without autoload-dev but I want to confirm first.
Solution: Composer autoload-dev does not work.
Take a look at the docs. It says: "autoload-dev (root only)". root only means it only applies to the root package. As you included the package, the shown composer.json file is not the root package and the autoload-dev section is thus ignored.

Class not working until composer dump-autoload

I created folder name - Birds and in this folder i created class Birds.php then add this to composer.json,
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Birds\\": "Birds/",
}
},
To load i have to run - composer dump-autoload -o and that's fine for first time to take that folder, But then i created new interface class like this :
namespace Birds\Validator;
interface BadgeInterface
{
public function test();
}
Interface class not working until in run composer dump-autoload,
My question is why i need to run this every time ? i am using laravel as framework.
Thanks
That's just how composer works. Laravel is just a PHP framework, so it is not able to run composer or other tool all the time for you.
If you need to automatically add files all the time, you can just use cron or Laravel's schedule command. It will do all the work for you every 5 minutes, for example.
This is cause when you create any class composer folder autoload.php can't update . that cause you need to run it manually . it clear you when you install any package at last command there run is composer autoload to update all reference in autoload.php file

Categories