RicLeP / laravel-storyblok

Make Laravel and Storyblok work together beautifully.
https://ls.sirric.co.uk/
MIT License
58 stars 15 forks source link

Resolve global reference #18

Closed WASDLauterbach closed 3 years ago

WASDLauterbach commented 3 years ago

Hey Richard! We have a global reference that we want to resolve. Is there a workflow to handle this?

For more information you can read here: https://www.storyblok.com/tp/global-components-references

Have a nice weekend!

RicLeP commented 3 years ago

At the moment I’ve been using view components for this: https://laravel.com/docs/8.x/blade#components

<?php

namespace App\View\Components;

use Illuminate\View\Component;
use Riclep\Storyblok\StoryblokFacade as StoryBlok;

class FooterMenu extends Component
{
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.footer-menu', [
            'menu' => Storyblok::read('navigation/footer-menu'),
        ]);
    }
}

I do think it would be a good idea for me to add global content in a simpler manner. Maybe some configuration where you specify slugs of pages you want always loaded - or maybe loaded based on path. I will look at this for a future release.

RicLeP commented 3 years ago

One other way of doing this would be to use your own controller instead of the packages built in ‘catch all’ controller route: https://github.com/RicLeP/laravel-storyblok/blob/master/src/Http/Controllers/StoryblokController.php

Change the catch all route: Route::get('/{slug?}', '\Riclep\Storyblok\Http\Controllers\StoryblokController@show')->where('slug', '(.*)'); to use your own controller.

You could replicate the show method but pass more data to render().

public function show($slug = 'home')
    {
        return Storyblok::read($slug)->render(
            'menu' => Storyblok::read('navigation/footer-menu'),
        );
    }

This should work too.

WASDLauterbach commented 3 years ago

I'll take a look :) Thank you, RicLeP!