astro_369's avatar

adding sharable link for facebook throws error?

To share posts in facebook, shareable trait looks like this...

trait Shareable
{
    public function getShareUrl($type = 'facebook')
    {
        $url = $this->{Arr::get($this->shareOptions, 'url')} ? $this->{Arr::get($this->shareOptions, 'url')} : url()->current();

        if ($type == 'facebook') {
            $query = urldecode(http_build_query([
                'app_id' => env('FACEBOOK_APP_ID'),
                'href' => $url,
                'display' => 'page',
                'title' => urlencode($this->{Arr::get($this->shareOptions, 'columns.title')})
            ]));

            return 'https://www.facebook.com/dialog/share?' . $query;
        }

In blade:

     href="{{ $post->getShareUrl('facebook')  }}" 

Configured app ID in .env but throws error..

Error: Invalid App ID: The provided app ID does not look like a valid app ID.

what am I missing?

0 likes
4 replies
J0wZ's avatar
J0wZ
Best Answer
Level 10

As a good practice you should have a file in config who uses env() and then use config()in your trait.'

Are you sure to have the good APP_ID ? Do you have many .env files depending of your app state ? (dev, prod, staging). If so, are you sur you put the FACEBOOK_APP_ID in the right .env file ?

astro_369's avatar

Thanks for reply! As long as I don't use

php artisan config:cache 
env() in Trait retrieves from .env.

I need to cache config files in production. So as per your suggestion, I created, shareable.php in config/ as

   env('FACEBOOK_APP_ID'),
      ];

And In the trait,

$query = urldecode(http_build_query([
                'app_id' => config('FACEBOOK_APP_ID'),
                ....

Still dont work , what am I missing?

MichalOravec's avatar

@astro_369

config/shareable.php

<?php

return [
  
    'facebook' => [
        'app_id' => env('FACEBOOK_APP_ID')
    ]

];

Then in the code use it as config('shareable.facebook.app_id');

1 like

Please or to participate in this conversation.