Laravel 5.6安装cartalyst/converter报错,寻求替代安装方案
Hey there, let's sort out this issue you're facing. The error you're seeing happens because Cartalyst Converter 2.0's Laravel Service Provider uses the deprecated share() method, which Laravel removed in versions 5.4+. Laravel 5.6 uses singleton() instead for binding shared instances.
Here are two solid solutions to get Converter working in your Laravel 5.6 app:
1. Fix the Official Service Provider (or Create a Custom One)
Instead of ditching the package entirely, you can update the Service Provider to use Laravel 5.6-compatible code:
Option A: Modify the Existing Provider
Locate the Cartalyst\Converter\Laravel\ConverterServiceProvider file in your vendor/cartalyst/converter/src directory. Find all instances of:
$app->share()
And replace them with:
$app->singleton()
This directly swaps the deprecated method with Laravel's current equivalent for shared bindings.
Option B: Create a Custom Service Provider
If you don't want to modify vendor files (which gets overwritten on composer update), create your own provider:
- Make a new file at
app/Providers/ConverterServiceProvider.phpwith this code:<?php namespace App\Providers; use Cartalyst\Converter\Converter; use Illuminate\Support\ServiceProvider; class ConverterServiceProvider extends ServiceProvider { public function register() { // Bind Converter as a singleton (shared instance) $this->app->singleton('converter', function ($app) { return new Converter; }); // Add an alias for easier access $this->app->alias('converter', Converter::class); } } - Open
config/app.php, remove the originalCartalyst\Converter\Laravel\ConverterServiceProviderfrom theprovidersarray, and add your new provider:App\Providers\ConverterServiceProvider::class,
Now you can use Converter like normal via dependency injection or the app('converter') helper.
2. Use the "Native" Installation in Laravel
If you prefer the native approach (no official Laravel integration), here's how to set it up in your app:
Step 1: Create a Config File
Add a config/converter.php file to store your default settings:
<?php return [ 'defaults' => [ 'length' => 'meter', 'weight' => 'kilogram', 'volume' => 'liter', 'currency' => 'USD', ], ];
Step 2: Bind the Converter Instance
Open app/Providers/AppServiceProvider.php and update the register() method to bind the Converter with your config:
use Cartalyst\Converter\Converter; public function register() { $this->app->singleton(Converter::class, function ($app) { $converter = new Converter; // Load default settings from your config file $defaults = $app['config']->get('converter.defaults'); foreach ($defaults as $type => $unit) { $converter->setDefault($type, $unit); } return $converter; }); }
Step 3: Use the Converter
You can now access the Converter anywhere in your app via dependency injection:
use Cartalyst\Converter\Converter; class ProductController extends Controller { public function show(Converter $converter) { // Convert 5 feet to meters $meters = $converter->convert(5)->from('foot')->to('meter'); // ... rest of your logic } }
Or using the app helper:
$converter = app(Converter::class);
Either of these methods should resolve the share() error and get Cartalyst Converter working in your Laravel 5.6 environment.
内容的提问来源于stack exchange,提问作者Yevgeniy Afanasyev

