PHP code example of cwsdigital / twill-metadata

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;
    }
}

{{-- config/twill.php --}
return [
    'settings' => [
        'crops' => [
            'default_social_image' => [
                'default' => [
                    [
                        'name' => 'default',
                        'ratio' => 1.91 / 1,
                        'minValues' => [
                            'width' => 1200,
                            'height' => 627,
                        ],
                    ],
                ],
            ],
        ],
    ]
];

{{-- app/Providers/AppServiceProvider.php --}}
public function boot(): void
{
    // Register Twill Settings
    TwillAppSettings::registerSettingsGroups(
        SettingsGroup::make()->name('seo')->label(trans('twill-metadata::form.titles.fieldset')),
    );

}

Route::get('{slug}', \App\Http\Controllers\Frontend\PageController::class)
    ->name('frontend.page')->where('slug', '.*');


// 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';
shell script
$ php artisan migrate
blade
{{-- resources/views/layouts/site.blade.php --}}
<html lang="en">
<head>

    {!! SEO::generate() !!}

</head>
shell script
  php artisan vendor:publish --provider="CwsDigital\TwillMetadata\TwillMetadataServiceProvider" --tag=config
shell script
  php artisan vendor:publish --provider="CwsDigital\TwillMetadata\TwillMetadataServiceProvider" --tag=views
shell script
  php artisan vendor:publish --provider="CwsDigital\TwillMetadata\TwillMetadataServiceProvider" --tag=lang