Alias and provider name unknown for github library - laravel

I am trying to install this github library:
https://github.com/robholmes/term-extractor
I used composer by adding this in composer.json under "require":
"robholmes/term-extractor": "dev-master"
However, I'm not sure what I would put in the app.php config file under providers and aliases. What would be the lines I would add there?

This is not a Laravel specific package. No need to add service providers, as they don't exist.
composer require robholmes/term-extractor dev-master
Instead, just include it in your controller for use, etc:
use TermExtractor\TermExtractor;
To test it works (it should), in your controller method just return this:
$term_extractor = new TermExtractor();
return dd($term_extractor);

Related

Laravel package class not found

I have installed laravel packages on staging server and packages working fine. But when i take pull on staging server, it is showing me error that package class not found.
Steps I have followed to resolve issue
I have check in vendor folder as well as in config/app.php, but I got class declaration and package folder is there.
After this when I update composer, my issue get resolved.
Is there any other file which should i look for class defination?
Perform a composer update, then composer dump-autoload.
If the above doesn't solve the problem, change the classmap in your composer.json file such that it contains the project-relative path to your php files:
"autoload-dev": {
"classmap": [
"tests/TestCase.php",
"database/seeds/UserTableSeeder.php" //include the file with its path here
]},
and soon after, perform a composer dump-autoload, and it should work now

Laravel use non composer package

How can I use this non composer package to my controller?
https://github.com/lelag/otphp
I've tried
use OTPHP;
$totp = new \OTPHP\TOTP($secret);
and I get
Class 'OTPHP\TOTP' not found
You will need to require the file.
require_once('/path/to/otphp.php');
Or if you want to use composer.json, look into using classmaps

how to create new module in 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.

Composer classmap autoload does not load new files in folder

The following problem: I have defined a classmap in my composer.json:
"autoload": {
"classmap": [
"app/controllers",
"app/models",
"app/helper.php"
]
}
However, when I create a new file in the "controllers" or "models" folder, it will not load them and I always have to make a composer dump-autoload.
Is this the correct behavior? I thought the autoloader from composer monitors the folder for new files then?
Yes, this is correct behaviour. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
Generating the classmap requires Composer to know the filename that contains a certain class. This can only be done by parsing the whole source code in the directory and scanning for classes, interfaces and trait definitions.
This usually is a CPU and I/O intensive task, so it is only done when Composer does install/update or (on demand) dumps the autoloader, it is not done with every require "vendor/autoload.php";.
Note that the classmap autoloading is simply there for old legacy codebases that didn't implement at least PSR-0. It is not intended for new code - unless you want to pay the price to dump the autoloader again and again during development.
Go to the root of your server by SSH. Now do the following:
Run ls to list all the files.
You will see composer.lock file; remove the file with rm composer.lock command.
Now run php composer update command.
Depending on your linux type you may have to run php-cli composer update.
Step 3 will create a new composer.lock file and all your classes will be loaded again. Do this anytime you add new classes.
or:
Run composer dump-autoload command.
As already pointed out this is correct behavior. If you want new classes to be loaded automatically, you have to use either PSR-0 or PSR-4 autoloading.
The classmap autoload type specified is composer.json is mainly used by legacy projects that do not follow PSR-0 or PSR-4. I have recently started working on such a project and wanted to try to automatically run the composer dump-autoload command when a new class is created. This is actually tricky without including all of the composer source inside the project. I came up with this just to remind the developer they need to dump the classmap:
$loader = include_once 'vendor/autoload.php';
if ( ! $loader ) {
throw new Exception( 'vendor/autoload.php missing please run `composer install`' );
}
spl_autoload_register(
function ( $class ) {
if ( 'A_Common_Class_Prefix' === substr( $class, 0, 10 ) ) {
throw new Error( 'Class "' . $class . '"" not found please run `composer dump-autoload`' );
}
},
true
);
This registers another autoloader which is run after composer's autoloader so any classes composer did not find would be passed to it. If the class matches a prefix an exception is throw reminding the developer to re-dump the autoloader and update the classmap.
For me, it somehow did not work too with Yii 1 class-map, when I added - required it along with many other libraries present - I don't remember exactly perhaps I manually edited the file or file permissions were to blame, it was not regenerated for some reason, even when I removed the composer.lock and erased completely the vendor folder - perhaps some cache, as far as I remember, but effectively what helped was to install firstly isolatedly only this single library, it generated class-map, then I added all the other remaining libraries separately at once at second step, viola, everything loadable.

Setting up alias for master branch in composer

I am trying to package a module for use with composer. I have a valid composer file and I am able to install it with composer, but only when I specify that it should use dev releases (via the "#dev" version directive or minimum stability dev). I am having trouble packaging my repo so that it is seen as a master release.
I found a composer document about aliases that seems to be made for my case, but I cant get it working. Heres the relevant portion of my composer.json:
"extra": {
"branch-alias": {
"dev-master": "1.0"
}
}
Also for reference heres the require from my main projects composer file:
"require": {
"misterglass/kohana-twig" : "1.*"
},
And the actual error from composer is:
Problem 1
- The requested package misterglass/kohana-twig 1.* could not be found.
According to some helpful people on the #composer IRC channel, aliases are just to associate different versions to each other, and not to assign stability.
In order to for composer to consider it stable, you need to add a tag, which you can do on the command line or by creating a release in github.

Resources