View Components & Views

View Components & Views

Views are quickly explained. In Laravel, views refers to the blade template files that are created in the respective package under:

Structure Views

                • action-message.blade.php
      • There are two different types of views. On the one hand, the view components and on the other hand the views respectively Anonymous Views.

        For both types, the view file is created under packages/your-package/resources/views/components/ components.

        and called with the package-name as prefix <x-your-package::the-view />.

        Anonymous Views

        
        <x-your-package::without-component-view-class/>
        echo view('your-package::without-component-view-class')->render();
        ⚠️
        The anonymous views without a class are used with double colon:

        load views aka blade

        Load the views integrates the package layouts into laravel. This allows these layouts to be output in Laravel.

        <?php
        namespace YourCompany\YourPackage\Providers;
        
        use Filefabrik\Bootraiser\Raiser;
        use Illuminate\Support\ServiceProvider;
        
        class YourPackageServiceProvider extends ServiceProvider
        {
            public function boot(): void
            {
                Raiser::forProvider($this)->loadViews();
            }
        }

        publish package views aka blade

        If you or the developer allows the user/admin to make changes to the templates, then also use publish.

        With publish, the original package files do not have to be changed, only the overrides.

        <?php
        namespace YourCompany\YourPackage\Providers;
        
        use Filefabrik\Bootraiser\Raiser;
        use Illuminate\Support\ServiceProvider;
        
        class YourPackageServiceProvider extends ServiceProvider
        {
            public function boot(): void
            {
                Raiser::forProvider($this)->publishViews();
            }
        }

        publish command

        php artisan vendor:publish

        For the package “your-package” the package views are suggested for publishing

        command suggestion

        The published files have been moved to the directory resources/views/vendor/your-package directory and can be edited in this directory without changing the package files (otherwise the changes would be lost after a package update).

        published views result

        Livewire views

        If Livewire is present in the package, these livewire-views are copied to the

        laravel-installation/resource/views/vendor/your-package/ directory with the

        php artisan vendor:publish command.

        See Livewire section

        View Components

        For view-components that are included in layouts, laravel tries to load a component class, not the layout file. The view class renders the layout with the render() method

        <x-your-package-an-custom-view-component />
        ⚠️
        View classes are written through or connected with a hyphen instead of the double colon:

        To make these package view classes available in laravel, the following setting must be made in bootraiser:

        load view component class

        <?php
        namespace YourCompany\YourPackage\Providers;
        
        use Filefabrik\Bootraiser\Raiser;
        use Illuminate\Support\ServiceProvider;
        
        class YourPackageServiceProvider extends ServiceProvider
        {
            public function boot(): void
            {
                Raiser::forProvider($this)->loadComponents();
            }
        }

        FilamentPHP Views

        If you are using a FilamentPHP view or a page with layout in your package, change the naming from:

        <?php 
        protected static string $view = 'your-vendor.your-package.filament.pages.my-page';
        ?>
        

        to:

        <?php 
        protected static string $view = 'your-package::filament.pages.my-page';
        ?>
        

        References

        https://laravel.com/docs/11.x/packages#view-components

        https://laravel.com/docs/11.x/packages#views

        Last updated on