在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):spatie/schema-org开源软件地址(OpenSource Url):https://github.com/spatie/schema-org开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):A Fluent Builder For Schema.org Types And ld+json Generator
use Spatie\SchemaOrg\Schema;
$localBusiness = Schema::localBusiness()
->name('Spatie')
->email('[email protected]')
->contactPoint(Schema::contactPoint()->areaServed('Worldwide'));
echo $localBusiness->toScript(); <script type="application/ld+json">
{
"@context": "https:\/\/schema.org",
"@type": "LocalBusiness",
"name": "Spatie",
"email": "[email protected]",
"contactPoint": {
"@type": "ContactPoint",
"areaServed": "Worldwide"
}
}
</script> Support usWe invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products. We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall. InstallationYou can install the package via composer: composer require spatie/schema-org UsageAll types can be instantiated through the $localBusiness = Schema::localBusiness()->name('Spatie');
// Is equivalent to:
$localBusiness = new LocalBusiness();
$localBusiness->name('Spatie');
All types also implement the SPL's $anotherLocalBusiness = new LocalBusiness();
var_dump(isset($anotherLocalBusiness['name'])); // => false
$anotherLocalBusiness['name'] = 'Spatie';
var_dump(isset($anotherLocalBusiness['name'])); // => true
var_dump($anotherLocalBusiness['name']); // => 'Spatie'
unset($anotherLocalBusiness['name']);
var_dump(isset($anotherLocalBusiness['name'])); // => false Types can be converted to an array or rendered to a script. $localBusiness->toArray();
echo $localBusiness->toScript();
echo $localBusiness; // Same output as `toScript()` Additionally, all types can be converted to a plain JSON string by just calling echo json_encode($localBusiness); I recommend double checking your structured data with Google's structured data testing tool. EnumerationsAs of v1.6.0, all Enumeration child types are available as classes with constants. Schema::book()->bookFormat(Spatie\SchemaOrg\BookFormatType::Hardcover); There's no full API documentation for types and properties. You can refer to the source or to the schema.org website. If you don't want to break the chain of a large schema object, you can use the use Spatie\SchemaOrg\LocalBusiness;
use Spatie\SchemaOrg\Schema;
$business = ['name' => 'Spatie'];
$localBusiness = Schema::localBusiness()
->name($business['name'])
->if(isset($business['email']), function (LocalBusiness $schema) use ($business) {
$schema->email($business['email']);
}); IdentifierAs of v2.6.0 the
Advanced UsageIf you'd need to set a custom property, you can use the $localBusiness->setProperty('foo', 'bar'); If you'd need to retrieve a property, you can use the $localBusiness->getProperty('name'); // 'Spatie'
$localBusiness->getProperty('bar'); // null
$localBusiness->getProperty('bar', 'baz'); // 'baz' All properties can be retrieved as an array with the $localBusiness->getProperties(); // ['name' => 'Spatie', ...] Multiple properties can be set at once using the $localBusiness->addProperties(['name' => 'value', 'foo' => 'bar']); Context and type can be retrieved with the $localBusiness->getContext(); // 'https://schema.org'
$localBusiness->getType(); // 'LocalBusiness' Graph - multiple itemsThe Graph has a lot of methods and utilities - the type-safe and simplest way is to use the overloaded methods of the $graph = new Graph();
// Create a product and prelink organization
$graph
->product()
->name('My cool Product')
->brand($graph->organization());
// Hide the organization from the created script tag
$graph->hide(\Spatie\SchemaOrg\Organization::class);
// Somewhere else fill out the organization
$graph
->organization()
->name('My awesome Company');
// Render graph to script tag
echo $graph; With these tools the graph is a collection of all available schemas, can link these schemas with each other and prevent helper schemas from being rendered in the script-tag. Graph Node identifiersSometimes you have to keep track of multiple Graph nodes of the same type - for example multiple use Spatie\SchemaOrg\Graph;
use Spatie\SchemaOrg\Person;
$graph = new Graph();
// add a Person using chaining
$graph->person('freekmurze')
->givenName('Freek')
->familyName('Van der Herten')
->alternateName('freekmurze');
// add a Person using closure
$graph->person('sebastiandedeyne', function(Person $sebastian, Graph $graph): void {
$sebastian
->givenName('Sebastian')
->familyName('De Deyne')
->alternateName('sebastiandedeyne');
});
// add a person using closure and second call with same identifier
$graph->person(
'gummibeer',
fn(Person $gummibeer) => $gummibeer->alternateName('gummibeer')
);
$graph->person('gummibeer')
->givenName('Tom')
->familyName('Witkowski');
$graph->person('random')->name('Random Person');
// hide the random person from Graph
$graph->hide(Person::class, 'random');
echo json_encode($graph); {
"@context":"https:\/\/schema.org",
"@graph":[
{
"@type":"Person",
"givenName":"Freek",
"familyName":"Van der Herten",
"alternateName":"freekmurze"
},
{
"@type":"Person",
"givenName":"Sebastian",
"familyName":"De Deyne",
"alternateName":"sebastiandedeyne"
},
{
"@type":"Person",
"alternateName":"gummibeer",
"givenName":"Tom",
"familyName":"Witkowski"
}
]
} Multi Typed EntitiesSchema.org allows multi typed entities - to use them with this package you can use the $mte = new MultiTypedEntity();
$mte->hotelRoom()->name('The Presidential Suite');
$mte->product()->offers(
Schema::offer()
->name('One Night')
->price(100000.00)
->priceCurrency('USD')
);
$mte->product(function (Product $product) {
$product->aggregateRating(
Schema::aggregateRating()
->bestRating(5)
->worstRating(4)
);
});
echo json_encode($mte); {
"@context":"https:\/\/schema.org",
"@type":[
"HotelRoom",
"Product"
],
"name":"The Presidential Suite",
"offers":{
"@type":"Offer",
"name":"One Night",
"price":100000,
"priceCurrency":"USD"
},
"aggregateRating":{
"@type":"AggregateRating",
"bestRating":5,
"worstRating":4
}
} There isn't a real rule in place how the properties are merged. It only uses Known Issues
ChangelogPlease see CHANGELOG for more information what has changed recently. Testing$ composer test ContributingPlease see CONTRIBUTING for details. SecurityIf you've found a bug regarding security please mail [email protected] instead of using the issue tracker. PostcardwareYou're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. We publish all received postcards on our company website. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论