butschster / LaravelMetaTags

The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
MIT License
540 stars 50 forks source link

Target [Butschster\Head\Contracts\MetaTags\MetaInterface] is not instantiable. #12

Closed xLuisCumbi closed 4 years ago

xLuisCumbi commented 4 years ago

I'm getting this error in LAravel 6 Target [Butschster\Head\Contracts\MetaTags\MetaInterface] is not instantiable.

Please help

butschster commented 4 years ago

Hi. I need more information about the problem. When did you get this error?

There was a problem in older version and it was fixed https://github.com/butschster/LaravelMetaTags/commit/b1228fef55227dc519fe8efb25cdef21358ac9bc

The problem caused only when console commands were run. Maybe do you have this code

public function register()
{
        // This condition will be removed
        if (!$this->app->runningInConsole() || $this->app->runningUnitTests()) {
            $this->registerPackageManager();
            $this->registerMeta();

            $this->packages();
        }
}

in the service provider App\Providers\MetaTagsServiceProvider.

The register method should look like over there https://github.com/butschster/LaravelMetaTags/blob/master/src/Providers/MetaTagsApplicationServiceProvider.php#L25

xLuisCumbi commented 4 years ago

There is no Register method in App\Providers\MetaTagsServiceProvider. This is the code in that class:

protected function packages()
    {
        // Create your own packages here
    }

    // if you don't want to change anything in this method just remove it
    protected function registerMeta(): void
    {
        $this->app->singleton(MetaInterface::class, function () {
            $meta = new Meta(
                $this->app[ManagerInterface::class],
                $this->app['config']
            );

            // It just an imagination, you can automatically
            // add favicon if it exists
            // if (file_exists(public_path('favicon.ico'))) {
            //    $meta->setFavicon('/favicon.ico');
            // }

            // This method gets default values from config and creates tags, includes default packages, e.t.c
            // If you don't want to use default values just remove it.
            $meta->initialize();

            return $meta;
        });
    }
butschster commented 4 years ago

And what about version? When did you get this error?

xLuisCumbi commented 4 years ago

I'll try to explain what's happening.

I followed the install instructions

  1. Command: Composer require...
  2. Command: meta-tags:install ...
  3. The MetaTagsServiceProvider exists

And i created this class:

use Butschster\Head\MetaTags\MetaInterface;
use App\Repositories\BlogsRepository;

class BlogsController extends AppBaseController
{
    /** @var  BlogsRepository */
    private $blogsRepository;
    protected $meta;

    public function __construct(BlogsRepository $blogsRepo, MetaInterface $meta)
    {
        $this->meta = $meta;
        $this->blogsRepository = $blogsRepo;
    }
public function blogPosts()
    {
        $blogPosts = Blogs::where('status', '=', 'Active')->paginate(4);

        $data['blogPosts'] = $blogPosts;
        $this->meta

            // Will render "Home page - Default Title"
            ->prependTitle('Home page')

            // Will include next, prev, canonical links
            ->setPaginationLinks($data)

            // Will change default favicon
            ->setFavicon('/favicon-index.ico');
        return view('front.blog.blogPosts')->with($data);
    }

Now I'm getting this error:

Class Butschster\Head\MetaTags\MetaInterface does not exist

What I'm doing wrong? I really wants to use this library, but please help me. Let me know if you need more information. Thanks in advance

xLuisCumbi commented 4 years ago

Will be awesome if you have a full example/tutorial.

butschster commented 4 years ago

There can be two reasons.

  1. Problems in App\Providers\MetaTagsServiceProvider, but you don't have any.
  2. This service provider was not add to the providers array in your app config/app.php
sabbirahmed395 commented 4 years ago

@Luismij If your are in same situation, remove app/Providers/MetaTagsServiceProvider.php, App\Providers\MetaTagsServiceProvider::class, line from config/app.php and config/meta_tags.php and run composer remove butschster/meta-tags. Install again. After installation run php artisan optimize:clear. This procedure works for me.

jrson83 commented 4 years ago

@majorsabbir Thank you!!! I had the same issue after installing on Laravel 7. I followed your instructions and it fixed the error.

xLuisCumbi commented 4 years ago

Thank you I'll try it and let you know if works for me

livevsonline commented 2 years ago

@majorsabbir I still have this problem also tried uninstalling and all (found out App\Providers\MetaTagsServiceProvider::class) wasent added to my cofig/app.php

sabbirahmed395 commented 2 years ago

@livevsonline make sure you uninstall LaravelMetaTags first. run php artisan config:clear, then follow the installation process again Laravel Meta Tags Installation. if it still doesn't work run php artisan optimize. Note: After running php artisan meta-tags:install command if you found there is no meta-tags.php in config folder, check your current user permission.