Looking to hire Laravel developers? Try LaraJobs

laravel-view-directives maintained by m2collective

Description
This package simplifies and systematizes the creation and registration of user directives.
Author
Last update
2026/07/24 08:46 (dev-main)
License
Downloads
1

Comments
comments powered by Disqus

View Directives

This package simplifies and systematizes the creation and registration of user directives.

Laravel PHP


Installation

You can install the package via composer:

composer require m2collective/laravel-view-directives

The package will automatically register itself.

Usage

By installing the package, you can create and register directives in a simpler and more intuitive way.

Create Directive

Creating a directive for displaying or formatting incoming arguments.

use M2Collective\ViewDirectives\Contracts\ClosingDirective;
use M2Collective\ViewDirectives\Contracts\LogicalDirective;
use M2Collective\ViewDirectives\Contracts\OpeningDirective;

final class Example implements OpeningDirective, LogicalDirective, ClosingDirective
{
    /**
     * @return string
     */
    public function openingTag(): string {
        return 'openingExample';
    }
    
    /**
     * @param mixed $expression
     * @return string
     */
    public function openingHandler(mixed $expression) : string
    {
        //...
    }
    
    /**
     * @return string
     */
    public function logicalTag(): string {
        return 'logicalExample';
    }
    
    /**
     * @param mixed $expression
     * @return string
     */
    public function logicalHandler(mixed $expression) : string
    {
        //...
    }
    
    /**
     * @return string
     */
    public function closingTag(): string {
        return 'closingExample';
    }
    
    /**
     * @param mixed $expression
     * @return string
     */
    public function closingHandler(mixed $expression) : string
    {
        //...
    }
}

Registering Directives

New directives are registered through the service provider.

use M2Collective\ViewDirectives\Providers\RegisterDirectives;

final class ExampleServiceProvider extends ServiceProvider
{
    use RegisterDirectives;
    
    /**
     * @return void
     */
    public function boot(): void
    {
        $this->registerDirective(
            new ExampleDirective()
        );
        
        // or
        
        $this->registerDirectives([
            new ExampleDirective(),
            //...
        ]);
    }
}

License

The MIT License (MIT). Please see the License file for more information.