unckleg/laravel-helpers

Laravel 辅助工具包允许您使用和创建自定义视图/动作辅助工具。

1.0.1 2017-06-12 07:24 UTC

This package is not auto-updated.

Last update: 2024-09-23 17:12:19 UTC


README

视图 & 动作辅助工具

Version License

Laravel 辅助工具包允许您使用和创建自定义视图/动作辅助工具。

安装

拉取包

composer require unckleg/laravel-helpers

在 config/app 中注册服务提供者

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

示例

创建 app/Helpers 目录或运行命令

php artisan make:helper Hello --type=View
  • Hello - 视图辅助工具名称
  • 类型 - 视图或动作

命令将为您创建目录和辅助工具。

  • 辅助工具
<?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 }
}
  • 在 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

内置辅助工具

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

注意

如果在使用命令时遇到权限错误,请确保已授予 Helpers 目录的权限。