You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.6安装cartalyst/converter报错,寻求替代安装方案

Fixing Call to undefined method Illuminate\Foundation\Application::share() in Cartalyst Converter 2.0 with Laravel 5.6

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:

  1. Make a new file at app/Providers/ConverterServiceProvider.php with 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);
        }
    }
    
  2. Open config/app.php, remove the original Cartalyst\Converter\Laravel\ConverterServiceProvider from the providers array, 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:37:16