1. Go to this page and download the library: Download cwsdigital/twill-metadata library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cwsdigital / twill-metadata example snippets
// App/Models/Page.php
class Page extends Model {
use HasMetadata;
use HasMedias;
public \Illuminate\Contracts\Foundation\Application|array|\Illuminate\Config\Repository|\Illuminate\Foundation\Application $metadataFallbacks = [];
...
}
// App/Http/Controllers/Admin/PageController.php
public function getForm(TwillModelContract $model): Form
{
$form = parent::getForm($model);
// add your fields here...
// copy the below to ces\Forms\BladePartial::make()->view('twill-metadata::orm;
}
// App/Repositories/PageRepository.php
class PageRepository extends ModuleRepository
{
use HandleBlocks, HandleSlugs, HandleMedias, HandleFiles, HandleRevisions, HandleMetadata;
public function __construct(Page $model)
{
$this->model = $model;
}
}
// App/Http/Controllers/PageController.php
class PageController extends Controller
{
use SetsMetadata;
public function __invoke(string $slug, \App\Repositories\PageRepository $pageRepository): \Illuminate\View\View
{
$page = $pageRepository->forSlug($slug);
abort_if(! $page, 404);
// Set the page metadata
$this->setMetadata($page);
// return your view
return view('site.pages.page', ['page' => $page]);
}
}
// Key is the metadata attribute,
// Value is the model attribute it will fall back to if metadata value is empty
'fallbacks' => [
'title' => 'title',
'description' => 'content',
'og_type' => 'metadataDefaultOgType',
'card_type' => 'metadataDefaultCardType',
],
// App/Models/Page.php
class Page extends Model {
use HasMetadata;
public $metadataFallbacks = [
'title' => 'name',
'description' => 'bio',
];
...
}
public $metadataDefaultOgType = 'website';
public $metadataDefaultCardType = 'summary_large_image';