unckleg/laravel-helpers

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

1.0.1 2017-06-12 07:24 UTC

This package is not auto-updated.

Last update: 2024-11-18 17:48:17 UTC


README

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

  • @javascripts()  @includeJs()    @endjavascripts()
  • @stylesheets() @includeCss() @endstylesheets()
  • @routeName()

Notes

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