how to create new module in Laravel - laravel

Is there any simple way to create new module in Laravel, if yes then please explain in detail.
Need to create new custom module in Laravel for users, product etc but didnt understand how to proceed with the same.

These packages will not help you if you don't understanding what exactly are you doing. I really recommend you to start with this tutorial, for example.

You should be right to go with
php artisan module:make <module_name>
You can also create multiple modules at once doing this:
php artisan module:make Users Products
this command will create all the resources you need like controller, seed class service provider and scaffold all the routes. If you dont want all this create new module with the following command
php artisan module:make Products --plain

After research found solution on GITHub :
https://github.com/jaiwalker/setup-laravel5-package
This might be better way to create a new package.
If anybody have better solution, then please share.

To install through Composer, by run the following command:
composer require nwidart/laravel-modules
The package will automatically register a service provider and alias.
Optionally, publish the package's configuration file by running:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
By default the module classes are not loaded automatically. You can autoload your modules using psr-4 (composer.json). For example :
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
Creating a module is simple and straightforward. Run the following command to create a module.
php artisan module:make <module-name>
It is also possible to create multiple modules in one command.
php artisan module:make Blog User Auth

You can refer this open source project to create module https://asgardcms.com/ - A modular multilingual CMS built with Laravel 5.

Related

Cannot locate class of installed package

I'm writing a laravel package which contains spatie/laravel-sitemap.
I already included several external packages and I didn't encountered any issues, but for some reason I'm not able to integrate this one.
What I did is the usual:
composer require spatie/laravel-sitemap
Then I have created a Console command that have as handle method the following content:
public function handle()
{
SitemapGenerator::create(config('app.url'))
->configureCrawler(function (Crawler $crawler) {
$crawler->ignoreRobots();
})
->writeToFile(public_path('sitemap.xml'));
$this->line('<info>Sitemap generated');
}
when I execute the command registered as:
php artisan myapp:sitemap
I get:
Class "Spatie\Sitemap\SitemapGenerator" not found
The reference imported are:
use Spatie\Crawler\Crawler;
use Spatie\Sitemap\SitemapGenerator;
I also tried composer update and composer dump-autoload, same problem.
Any help?
register package class in providers array in config/app.php
Spatie\Sitemap\SitemapServiceProvider;
in the bottom of app.php file
i hope it was useful.
you can publish package using this.
php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=sitemap-config
then
composer dump-autoload
for more details please check the document https://github.com/spatie/laravel-sitemap under Configuration

PhpStorm can't find Laravel helpers

I use PhpStorm 2018.1.4. I installed the package laravel-ide-helper. This allows PhpStorm to see model methods, but it doesn't see any chaining methods from Laravel helpers. For example, I have the following code in the controller:
return response()->file($path,['content-type' => 'application/pdf']);
PhpStorm says to me:
Method 'file' not found in \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response.
How to fix it?
UPD: Of course, after install the package, I ran thees artisan commands:
php artisan ide-helper:generate
php artisan ide-helper:meta
Installing the laravel-ide-helper alone doesn't do anything. You need to run the artisan commands to generate the files that phpstorm will use.
php artisan ide-helper:generate
and
php artisan ide-helper:meta
Will help phpStorm's auto-completion.
Update: Since these are executed, the actual problem is a laravel structure issue:
The helper file() doesn't actually exist in the ResponseFactory (response() returns result of ResponseFactory) so IDE helper can't map to it.
file() does however exist in the Facade so if you were to do:
\Response::file($path,['content-type' => 'application/pdf']) it will auto complete.
It's a work around, but unless file() gets added to the response factory at laravel's level, there's not much ide helper or phpstorm can do.

There are no commands defined in the "backup" namespace

I have tried installing Laravel Backup for Database paulvl/backup
I have followed the instructions on how to install and configure it to my Laravel application. I am however stuck at the point where you actually run the backup command. I get this error:
$ php artisan backup:mysql-dump
[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "backup" namespace.
Any help would highly be appreciated
Here is the provider array in config/app.php
From the package docs:
Once the package's installation completes, the final step is to add the service provider. Open config/app.php, and add a new item to the providers array:
Backup\BackupServiceProvider::class,
These commands will be automatically registered since the BackupServiceProvider does that:
public function register()
{
$this->registerMysqlDumpCommand();
$this->registerMysqlRestoreCommand();
}

Laravel5.5 after use php artisan app:name Class not found

I am new developer on Laravel, now I'm using Laravel version 5.5
I got the problem after used php artisan app:name on my project, I got the problem:
In ProviderRepository.php line 208: Class
'App\Providers\AppServiceProvider' not found
as the captured image below:
As this error, I can not use php artisan commands or composer commands anymore can you guys please help me to solve this problem I am really appreciated for time. Thanks you
Best Regards
Siripong Gaewmaneechot
I would suggest looking in your config files, and the main classes which were generated when you started your laravel project (User class, etc) because they are all set to App\User App..... etc.
So for example, in the image you have in the question, it says it can not find App\AppProviders... - This indicates that somewhere you still have a use statement pointed to App\AppProviders.. but you changed the app name, so it's no longer App. something I do if I made that mistake, is I do a global search in my project files for App\ (you may need to put App\\ in the search because \ is an escape character
So if you did not change the app name immediately after starting the project, some of the paths will not be pointing to the correct directories. Let me know if that makes sense.
The command changes the PSR-4 configuration in composer.json.
Assume it was App before, your composer.json looks like this:
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
After running the command with php artisan app:name Foo, it will look like:
"autoload": {
"psr-4": {
"Foo\\": "app/"
}
},
Therefore the whole namespace has changed and your classes can't be found by the Autoloader. To fix this, you have to either go back to the old name or do a global search and replace to change the namespace from App to Foo.

How to use artisan to make views in laravel 5.1

I have been looking in the docs for a solution to make views with basic CURD operations but without much success.
I guess this might be pretty simple, but am missing something or not looking hard enough.
i can make models and controllers using the below artisan commands
php artisan make:model modelName
php artisan make:controller controllerName
But how do we make the basic CURD views. something like
php artisan make:views
cant find any doc for this. Please help
At the time of writing, there isn't a way to create views via artisan without writing your own command or using a third party package. You could write your own as already suggested or use sven/artisan-view.
if you are using laravel 5.1 or 5.2 this gist can help you make:view command just create command copy and paste the code from gist.
Step 1:
php artisan make:command MakeViewCommand
Step 2:
copy class from this gist
https://gist.github.com/umefarooq/ebc617dbf88260db1448
Laravel officially doesn't have any Artisan cammands for views.
But you could add third party plugins like Artisan View
Here's the link Artisan View
After adding this plugin to your project by the guide provided here you should be able to perform following cammands :
Create a view 'index.blade.php' in the default directory
$ php artisan make:view index
Create a view 'index.blade.php' in a subdirectory ('pages')
$ php artisan make:view pages.index
Create a view with a different file extension ('index.html')
$ php artisan make:view index --extension=html
There is very easy way to create a view(blade) file with php artisan make:view {view-name} command using Laravel More Command Package.
First Install Laravel More Command
composer require theanik/laravel-more-command --dev
Then Run
php artisan make:view {view-name}
For example
It create index.blade.php in resource/views directory
php artisan make:view index
It create index.blade.php in resource/views/user directory
php artisan make:view user/index
Thank you.
In v5.4 you need to create the command with:
php artisan make:command MakeView
and before you can use it, it must be registered in App/Console/Kernel like
protected $commands = [
Commands\MakeView::class
];
then you make a view like: php artisan make:view posts/create
To create a view (blade) file through command in laravel 8:
composer require theanik/laravel-more-command --dev
php artisan make:view abc.blade.php
You can install sven/artisan-view package to make view from CMD, to install package write this command:
composer require sven/artisan-view --dev
After installing it, you can make a single view or folder with all views that contain {index-create-update-show}
To make a single file we using this command:
php artisan make:view Name_of_view
For example
php artisan make:view index
To make a folder that contain all resources index - create - update - show write name of folder that contain all this files for example:
php artisan make:view Name_of_Folder -r
For example:
php artisan make:view blog -r
-r is a shorthand for --resource you can write full name or shorthand to make resource.
you can extend yields from master page if master page inside in directory layouts we write command sith this format
php artisan make:view index --extends=layouts.master --with-yields
layouts is a directory this directory may be with a different name in your project the idea is name_of_folder/master_page that you want to extend yields from it.
For more view docs

Resources