artesaos / seotools

SEO Tools for Laravel
MIT License
3.1k stars 505 forks source link

generating the JsonLD array having multiple JsonLD objects #204

Closed vkiranmaniya closed 3 years ago

vkiranmaniya commented 4 years ago

generating the JsonLD array having multiple JsonLD objects as given,

[ 
   { 
      "@context":"http://schema.org",
      "@type":"Product",
      "aggregateRating":{ 
         "@type":"AggregateRating",
         "ratingValue":3.7,
         "reviewCount":8034
      },
      "brand":{ 
         "@type":"Thing",
         "name":"Anand Sarees"
      },
      "image":"http://rukmini1.flixcart.com/image/128/128/k51cpe80/sari/m/z/t/free-printed-fashion-georgette-saree-multicolor-khodiyar-original-imaempjvcuqbhe2g.jpeg?q=70",
      "name":"Anand Sarees Striped Daily Wear Poly Georgette Saree"
   },
   { 
      "@context":"http://schema.org",
      "@type":"BreadcrumbList",
      "itemListElement":[ 
         { 
            "@type":"ListItem",
            "item":{ 
               "@id":"https://www.flipkart.com/womens-clothing/pr?sid=2oq,c1r&marketplace=FLIPKART&otracker=product_breadCrumbs_Women%27s+Clothing",
               "name":"Women's Clothing"
            },
            "position":3
         },
         { 
            "@type":"ListItem",
            "item":{ 
               "@id":"https://www.flipkart.com/womens-clothing/ethnic-wear/pr?sid=2oq,c1r,3pj&marketplace=FLIPKART&otracker=product_breadCrumbs_Ethnic+Wear",
               "name":"Ethnic Wear"
            },
            "position":4
         },
         { 
            "@type":"ListItem",
            "item":{ 
               "@id":"https://www.flipkart.com/sarees/anand-sarees~brand/pr?sid=2oq,c1r,3pj,7od&marketplace=FLIPKART&otracker=product_breadCrumbs_Anand+Sarees+Sarees",
               "name":"Anand Sarees Sarees"
            },
            "position":6
         }
      ]
   }
]

I want to generate the JsonLD array but didn't find the method to do it. Is it possible with this package or I need to modify? Let me know if there is no such method, I'll create a pull request for that.

hajar-alpha commented 4 years ago

I am interested in this too

vkiranmaniya commented 4 years ago

@hajar-alpha I tried to find the workaround, but haven't found yet. I think we should implement it on our own. I'll make changes and create pull request soon.

justindantzer commented 4 years ago

@vkiranmaniya I was able to have a working option to what I think is the goal here by changing some of the config options. Not sure if it is a perfect solution.

In config/seotools.php, I set title, description, and type to false, which prevents the default fields being output. This does however require specifying those fields where you would have made use of the defaults.

return [
    'meta' => [
        // ...
    ],

    // ...

    'json-ld' => [
        'defaults' => [
            'title'       => false,
            'description' => false,
            'url'         => false,
            'type'        => false,
            'images'      => [],
        ],
    ],
];

Then where I wanted to output the JsonLd, I used the addValue() method, with the named @graph:

(Stack overflow reference regarding @graph HERE)

JsonLd::addValue('@graph', [
    [
        '@type' => 'Organization',
        'name'  => 'Org name',
        // ...
    ],
    [
        '@type' => 'Website',
        'name'  => 'Website title',
        // ...
    ],
]);

The output JSON:

{
    "@context": "https:\/\/schema.org",
    "@graph": [
        {
            "@type": "Organization",
            "name": "Org name"
        },
        {
            "@type": "Website",
            "name": "Website title"
        }
    ]
}
vkiranmaniya commented 4 years ago

@vkiranmaniya I was able to have a working option to what I think is the goal here by changing some of the config options. Not sure if it is a perfect solution.

In config/seotools.php, I set title, description, and type to false, which prevents the default fields being output. This does however require specifying those fields where you would have made use of the defaults.

return [
    'meta' => [
        // ...
    ],

    // ...

    'json-ld' => [
        'defaults' => [
            'title'       => false,
            'description' => false,
            'url'         => false,
            'type'        => false,
            'images'      => [],
        ],
    ],
];

Then where I wanted to output the JsonLd, I used the addValue() method, with the named @graph:

(Stack overflow reference regarding @graph HERE)

JsonLd::addValue('@graph', [
    [
        '@type' => 'Organization',
        'name'  => 'Org name',
        // ...
    ],
    [
        '@type' => 'Website',
        'name'  => 'Website title',
        // ...
    ],
]);

The output JSON:

{
    "@context": "https:\/\/schema.org",
    "@graph": [
        {
            "@type": "Organization",
            "name": "Org name"
        },
        {
            "@type": "Website",
            "name": "Website title"
        }
    ]
}

Yes, this may be a workaround, Let's try it out.

BulgarianHealer commented 4 years ago

I was working also on complex jsonld, and it seems i made the same hacking as @justindantzer . This should be added in the readme as example for complex jsonld, so more people will know that this package is good and easy to use.

StApostol commented 3 years ago

@BulgarianHealer @vkiranmaniya @vinicius73 you can used my PR(#229) for next code

    $breadcumbsListItems = [
        new JsonLd(['type' => 'ListItem', ...]),
        new JsonLd(['type' => 'ListItem', ...]),
    ];

    $breadcrumbList = new JsonLd([
         'type' => 'BreadcrumbList',
         'itemListElement' => $breadcumbsListItems,
    ]);
vinicius73 commented 3 years ago

Release https://github.com/artesaos/seotools/releases/tag/v0.19.1