unckleg/laravel-helpers

Laravel helpers package lets you use and create custom view/action helpers.

Downloads

218

Stars

0

Version

1.0.1

View & Action Helpers

Version License


Laravel helpers package lets you use and create custom view/action helpers.

Installation

Pull package:

composer require unckleg/laravel-helpers

Register service provider in config/app

...
Unckleg\Helpers\HelpersServiceProvider::class,

Example

Create app/Helpers directory or run command:

php artisan make:helper Hello --type=View
  • Hello - Name of View Helper
  • type - View or Action

Command will create directory and Helper for you.

  • Helper
<?php

namespace App\Helpers;

class Test
{
    /**
     *
     * Blade calling: @test::helloWorld()
     *
     * @return string
     */
    public function helloWorld()
    {
        return 'Hello world';
    }

    /**
     *
     * Blade calling: @test::helloTo(array $people)
     *
     * @param  array  $people
     * @return string
     */
    public function helloTo(array $people)
    {
        return implode(', ', $people);
    }
    
    /**
     * 
     * Blade calling: @test::navigation()
     *
     * @return string   
     */
    public static function navigation() 
    { 
        $pages = App\Page::all();    
    ?>
        
        <div class="navigation">
            <ul> 
                @foreach($pages as $page)
                    <li> 
                        <a href="{{ Url::slugify(app()->baseUrl($page->title)) }}"> 
                            {{ $page->title }} 
                        </a>
                    </li>
                @endforeach
            </ul>
        </div>

    <?php }
}
  • In blade
@test:helloWorld()
// outputs: Hello world

@test::helloTo(['One', 'Two', 'Three', 'Four', 'Five'])
// outputs: One, Two, Three, Four, Five

@test::navigation()
// outputs: This will output whole html provided in navigation method

Built-in helpers

Notes

If you experience permission error while using commands make sure you grant permissions for Helpers directory.

unckleg

Author

unckleg