Custom package service provider cannot be found - php

I'm trying to develop a custom package using "workbench". It's a simple package that will create a repository to consume a different API we have. I plan on injecting this repository into my controllers of my application. Anyway, I created the package using "workbench". This creates the service provider. The Laravel docs say to add the service provider to my "providers" array in app.config. I've done this, but when I run composer update, I get an error saying that the class is not found, specifically: PHP Fatal error: Class 'MyVendor\MyPackage\ServiceProvider not found in /.. .. .. ../laravel/vendor/framework/src/Illuminate/Foundation/ProviderRepository.php. I thought I followed the docs correctly, what am I missing?

Simply running composer install inside the workbench directory resolves this issue.

Related

About using of packages that your composer library suggests

I am developing some composer php library.
And I want to provide library users to use class that uses some external package. I suggest this package in the relevant composer section.
Should I check that the composer's suggested package is really installed?
Will there be an error if the user just install my library without suggestions, but will not use this class that dependent from not installed package?
I checked some popular packages and seems to be they just use suggested packages as if they are already installed.
For example: https://github.com/Seldaek/monolog/blob/c861fcba2ca29404dc9e617eedd9eff4616986b8/src/Monolog/Handler/ElasticsearchHandler.php
This monolog class just uses Elasticsearch classes as usual, but Elasticsearch is a suggested package and may be not installed.
The handlers use the other packages as if they were installed - but keep in mind that Monolog does not use each and every handler automatically. If you define that such a handler (in your example: the one for ElasticSearch) is used, it is up to the user to know how to handle this.
As you can see in the constructor of that method, it is not even instantiable if you don't have a package for ElasticSearch installed - the code example in that class helps to understand this situation. So, no, this class is not usable without having another package installed, but the error will be thrown by your classloader. No need for the package to check for this

Laravel 5 - Can I use a Facade within a service provider without registering it?

I'm currently developing a set of packages/service providers to use as a sort of boilerplate for new web applications. I started developing some time ago and I never ran the packages through composer.
After I made a repository and added the package to a new Laravel application, it installed just fine, until it ran php artisan package:discover. The error was:
In breadcrumbs.php line 5:
Class 'Athena' not found
and it refers to this line:
Breadcrumbs::register( \Athena::getFacadeRoot()->route_name_prefix . '.dashboard.show', function ( $breadcrumbs ) {
in which \Athena:: is a facade within the package. I fixed the issue by adding the facade to the app.php file. The idea was to bootstrap applications easily, so my question is:
Can I use a facade within a package and still use the Laravel package discovery, without having to edit files when I create a new project?

Laravel 5 dynamically install a package

So I am relatively new to Laravel so i'm trying to seek some advice. I've read the documentation on packages provided by laravel. Now i understand you need a service provider to connect your package to laravel like so: Blog\BlogServiceProvider::class. Now what i am trying to do is have a control panel that installs packages straight off of packagist. Now i can manipulate the composer.json file quite easily in code. However, what i am trying to work around is declaring the service provider. I think it would be inconvenient if i have a dynamic installation but you have to declare the service provider yourself. Is there a way i can dynamically register the service provider in Laravel 5?
You can dynamically add service providers through the App facade:
App::register('App\Providers\SomeServiceProvider');

Laravel installing Algolia package

I am trying to install the Algolia laravel package but I am getting this error:
Trait 'App\AlgoliaEloquentTrait' not found
I follow the instructions under install, configuration and quickstart from this link:
https://github.com/algolia/algoliasearch-laravel#configuration
I simply added use AlgoliaEloquentTrait; to one of my models. What could I be doing wrong here?
You need to add
use AlgoliaSearch\Laravel\AlgoliaEloquentTrait;
at the beginning of your model.

Issue when using a package (pagseguro/php) with composer in a laravel app, apparently it is not loading or initializing itself

I have a laravel app and i want to use the pagseguro/php package.
I added it to the composer.json and updated. I can access the main class (PagSeguroPaymentRequest) without a problem.
At some point I have to call this:
PagSeguroConfig::getAccountCredentials();
But it throws an exception. After reading code around I thought on trying to init the library by myself and suddenly everything worked:
PagSeguroLibrary::init();
This method is inside the only php file in source/PagSeguroLibrary/
Shouldn't composer automatically execute this method? What is exactly "loading" a package? Is there anyway to fix this using composer only?
Thank you all.
Shouldn't composer automatically execute this method?
No, it shouldn't. Composer is a package and dependency manager program. It's job is to
Get PHP files into your vendor folder
If using those PHP files means you need other PHP files, get those other PHP files into your vendor folder
Setup things so that class files from the packages are correctly autoloaded in PHP (i.e. no need to require or include stuff yourself)
Composer packages work independent of frameworks. Someone could distribute a laravel service provider via a computer package, or someone could distribute code that doesn't know anything about Laravel. How each composer package works is up to the author (always read the README)
In the case of pagseguro/php, it looks like you're supposed to instantiate a PagSeguroPaymentRequest object which, when autoloaded, will automatically call init. The examples distributed with the package also makes it look like this package was code that predated composer, and still uses many manual includes and requires.

Categories