How to add some external package with nwidart/laravel-modules? - laravel

In Laravel 6/nwidart/laravel-modules 7 app I see file composer.json in root of my module
with lines :
{
"name": "nwidart/pages",
"description": "",
"authors": [
{
"name": "Nicolas Widart",
"email": "n.widart#gmail.com"
}
],
"extra": {
"laravel": {
"providers": [],
"aliases": {
}
}
},
"autoload": {
"psr-4": {
"Modules\\Pages\\": ""
}
}
}
I suppose that if I want to add some external package into my module(not the whole app)
I need to edit this file and run module commands?
How have I to edit it and which commands to use?

You can use regular composer commands in the module directory. Just cd into it then use composer require package-name.

After adding an entry to the compose file using e.g. in folder your module
composer require package-name
Use the command in the main application in folder global application
php artisan module:update name-your-module

Related

How to install github package in laravel 7

I am new in laravel 7. i want ask how to install github package into my laravel project.
(https://github.com/lomotech/jajahan) this package i want to install to my laravel but not instruction in this package.
If a package is missing a composer.json, you can add the following to your project's composer.json:
"repositories": [
{
"type": "package",
"package": {
"name": "lomotech/jajahan",
"version": "v2.2018.01",
"source": {
"url": "https://github.com/lomotech/jajahan",
"type": "git",
"reference": "master"
}
}
}
],
"require": {
"lomotech/jajahan": "*"
},
"autoload": {
"psr-4": {
"Lomotech\\Jajahan\\": "vendor/lomotech/jajahan/"
}
},
The autoload entry allows composer to generate the vendor classmaps so you can use the package in your code by:
use Lomotech\Jajahan\SomeClassFile;
Finally, run composer install/update.

Add command (console command) in a Prestashop module 1.7.6

I'm developing a prestashop module (v. 1.7.6) and I'd like to add some command available from console.
The problem is that when I configure services and try to run
bin/console mymodule:mycommand
system returns There are no commands defined in the "mymodule" namespace.
Here my services.yml
services:
mycommand_command:
class: Mymodule\MycommandCommand
tags:
- { name: 'console.command' }
Here my composer.json
{
"autoload": {
"psr-4": {
"Mymodule\\": "src/"
},
"classmap": [
"src/Command/"
]
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false
},
"type": "prestashop-module",
"author": "<???>",
"license": "<???>"
}
Any suggestions?
Thank you
Where have you create your Command class ? In src/Command/ ?
Update:
To myself:
When you Google this question next time - check:
Command defined in config/services.yml
The module is installed
Composer autoloder dumped
Old version:
Make sure you dumped autoloader in Composer and added protected static $defaultName = 'yourmodule:command'; to your command class and set the name any other way mentioned in Symfony docs.
I just faced the same issue a few days ago.
To anyone is looking for this:
Follow the step on this link: https://devdocs.prestashop.com/1.7/modules/concepts/commands/
Add composer.json IN THE ROOT OF YOUR MODULE with this code (and replace with your stuff..):
{
"autoload": {
"psr-4": {
"<YOUR-MODULE>\\": "src/"
},
"classmap": [
"src/Command/"
]
},
"config": {
"preferred-install": "dist",
"prepend-autoloader": false
},
"type": "prestashop-module",
"author": "<AUTHOR>",
"license": "<LICENSE>"
}
Connect via ssh, and cd into your module root
run composer dump-autoload -o --no-dev
Test your command by <ps-root>/bin/console your-module:command

How to add helpers in own laravel packages? (Call to undefined function)

In my composer.json I have written:
"autoload": {
"psr-4": {
"Pmochine\\MyOwnPackage\\": "src/"
},
"files": [
"src/helpers.php"
]
},
But somehow even after composer dump-autoload the functions are not loaded. I get "Call to undefined function". To create the package I used a package generator. Maybe it has something to do that it creates a symlink in the vendor folder?
Inside helpers I have written
<?php
if (! function_exists('myowntest')) {
function myowntest()
{
return 'test';
}
}
In the package service provider, try adding this:
// if 'src/helpers.php' does not work, try with 'helpers.php'
if (file_exists($file = app_path('src/helpers.php'))) {
require $file;
}
What you are doing is best practise and should work. I took the composer.json from barryvdh/laravel-debugbar as an example. https://github.com/barryvdh/laravel-debugbar/blob/master/composer.json
{
"name": "barryvdh/laravel-debugbar",
"description": "PHP Debugbar integration for Laravel",
"keywords": ["laravel", "debugbar", "profiler", "debug", "webprofiler"],
"license": "MIT",
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh#gmail.com"
}
],
"require": {
"php": ">=5.5.9",
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*",
"symfony/finder": "~2.7|~3.0",
"maximebf/debugbar": "~1.13.0"
},
"autoload": {
"psr-4": {
"Barryvdh\\Debugbar\\": "src/"
},
"files": [
"src/helpers.php"
]
}
}
My guess is that you are not requiring your own package the correct way in the main composer.json?
Just need to call in your main project
composer update you/your-package
Source
The only thing that has worked for me is to run composer remove <vendor>/<package> and require it back again. The files section was ignored otherwise.
This seems to happen while developing locally the package and making changes on the composer.json file.

L5.5 : How to auto discover package?

I want to create package and I following this tutorial https://devdojo.com/blog/tutorials/how-to-create-a-laravel-package
I want to use auto discover the new feature of L5.5, how step-3 should be? (what I need to write on laravel's composer.json)
Laravel’s custom package’s providers will be auto-discovered only if the package present in vendor folder, So for that we need to make our package installable via composer itself.
So we need to make our custom package should be installable via composer, for that set your applications composer file with minimum-stability as dev is must and we have to configure custom packages path.
"minimum-stability" : "dev",
"repositories": [
{
"type": "path",
"url": "./packages/suresh/calc/"
}
]
once you done that your package can be installed using composer require <vendor/package>, then it will configure auto discover as per your packages settings. Get the sample configurations for your package,
{
"name": "suresh/calc",
"description": "This demo for auto discover providers in laravel with custom package",
"authors": [
{
"name": "Suresh Veluamy",
"email": "sureshamk#gmail.com"
}
],
"minimum-stability": "stable",
"require": {},
"autoload": {
"psr-4": {
"Suresh\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Suresh\\Calc\\CalcServiceProvider"
]
}
}
}
For more information, i wrote a post, check it out here

Delegating to Local composer.json

With my clean Laravel 5.3 installation, I can run composer install to install the dependent packages.
Now, I've an internal package with its own composer.json, like below:
{
"name": "bar/foo",
"description": "A package for handling foo",
"licence": "MIT",
"authors": [
{
"name": "A. Foo",
"email": "a#foo.bar"
}],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": {
"Foo\\Bar\\": "packages/foo/Bar/src"
}
}
}
So I prefer to autoload from the package itself, instead of autoloading from the main composer.json.
My questions:
Running composer dumpa from packages/foo/Bar doesn't take effect for autoloading. After Generating autoload files, Laravel doesn't know namespace Foo\Bar
Is there a way to run composer dumpa for all recursive composer.jsons?
You need to add the following section to your global composer.json
"repositories": [
{
"type": "path",
"url": "packages/*/*"
}
]
You also need to add the packages to the require object in composer.json

Resources