Create Module in laravel 5.3 - php

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

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

Laravel custom class not found?

I'm getting the following error
"Class 'Martins\ArtisanGUI\ArtisanGUIServiceProvider' not found",
I already write it in config/app.php like Martins\ArtisanGUI\ArtisanGUIServiceProvider::class and recheck namespaces, and composer but I can't figure out what is wrong.
You're editing the composer file of your package, not your project.
As the package does not appear to be installed and managed by composer, you need to make the project's composer aware of this package.
You should be able to add a local vcs repository in your project's composer.json file:
"repositories": [
{
"type": "vcs",
"url": "packages/martins-74/artisangui"
}
],
"require": {
"martins-74/artisangui": "*"
}
This will make it so you won't have to duplicate any composer rules across both json files.
you have two composer.json files in you projects :
the root of you Laravel project
in your package root (packages/martins-74/artisangui)
in the first composer.json file (the laravel composer.json):
"psr-4": {
"App\\": "app/",
"Martines\\ArtisanGUI\\" : "packages/martins-74/artisangui/src"
}
the seconds composer.json file (your package composer.json):
"psr-4": {
"Martines\\ArtisanGUI\\": "src",
}
after that run composer dump-autoload
hope this work for you

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.

Laravel phpspec cannot be autoloaded

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

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