archtechx / laravel-seo

SEO package for Laravel
https://archte.ch/blog/introducing-laravel-seo
MIT License
291 stars 30 forks source link

Seo blade tag to set social image? #22

Closed tpetry closed 2 years ago

tpetry commented 2 years ago

The @seo blade tag can set e.g. the description like this @seo(['description' => 'foo']). But I didn't find any way to do the same for the social image? The following call (which I expected to work) resulted in an error:

ArchTech\SEO\SEOManager::set(): Argument #2 ($value) must be of type Closure|string|null, array given, called in /Users/tpetry/Documents/laravel-seo/src/SEOManager.php on line 95

@seo(['flipp' => ['blog', ['title' => 'Lorem Ipsum', 'description' => 'Lorem Ipsum, Lorem Ipsum']]])
```php

Is it not supported to set the social image with the `@seo` tag without returning any value or did I just miss the correct call?
stancl commented 2 years ago

Seems like @seo() uses seo()->set(), and Flipp needs to be used via seo()->flipp(). It then calls set() for 'image': https://github.com/archtechx/laravel-seo/blob/754b3936d0772a1d368240271ec8532353bac1e8/src/SEOManager.php#L180

tpetry commented 2 years ago

Sure, but flipp() requires 2 parameters, how to do that with the @seo array parameter?

stancl commented 2 years ago

Ah hmm, seems like @seo uses flipp() correctly, it doesn't call set(): https://github.com/archtechx/laravel-seo/blob/754b3936d0772a1d368240271ec8532353bac1e8/src/SEOServiceProvider.php#L34-L40

I think the right syntax is @seo('flipp', [ array data ]). When you pass an array to @seo, there's no first argument indicating that this is a flipp() call, so it goes straight to set().

tpetry commented 2 years ago

But @seo('flipp', [ array data ]) will output the image URL. I just want to define the social image on one view which is then used by the meta tags in the laout, exactly like @seo(['title' => 'Test123']).

To make it more clear. Here's an example:

@seo([
  'title' => 'Hello World',
  /* not possible */ 'flipp' => ['blog', ['headline' => 'Lorem Ipsum', 'teaser' => 'Lorem Ipsum, Lorem Ipsum']] 
])
<x-layout>Some content</x-layout>

The @seo tag seems to only support strings for values, but the flipp and previewify functions need more parameters.

stancl commented 2 years ago

Ahh I see. Yeah hmm so:

So the blade directive should probably check if a passed array has a flipp key, and then call the flipp() method and exclude it from the data passed to set()?

tpetry commented 2 years ago

That's a possible approach, but the same for previewify.

stancl commented 2 years ago

Perhaps your PR could check for the presence of flipp and previewify in this array ($args[0]) too?

https://github.com/archtechx/laravel-seo/blob/d12038f207b2f0588cf6f60b8a5706d478428ddf/src/SEOServiceProvider.php#L47-L52