sven/helpers

一组在 Laravel 应用程序中非常有用的辅助函数。

v1.2.0 2018-01-14 15:26 UTC

This package is auto-updated.

Last update: 2024-09-05 18:19:01 UTC


README

helpers

辅助函数

Latest Version on Packagist Total Downloads Software License Build Status StyleCI

这是一个用于 Laravel 应用的有用辅助函数集合。主要用于个人使用,但如果您觉得这些辅助函数有用,请随意使用!

安装

通过composer

$ composer require sven/helpers

或者在 composer.json 中将包添加到依赖项,并在命令行上运行 composer install 来下载包

{
    "require": {
        "sven/helpers": "^1.0"
    }
}

可用函数

所有可用函数都列在这里,包括它们的用法以及如何使用它们的示例。

active_route

此函数将返回当前是否在指定路由名称上,如果是则返回 true,否则返回 false

$isHome = active_route('home');

您还可以传入可选的 $positive$negative 值来返回

$isContact = active_route('contact', 'Yes!', 'No :(');

您还可以提供一个包含多个路由名称的数组而不是单个路由名称。如果当前路由与给定的任何一个匹配,则函数将返回 $positive,否则返回 $negative

$isContactOrAbout = active_route(['contact', 'about']);

active_route() 对于例如在 blade 模板中的导航的 active 状态非常有用

<nav>
    <ul>
        <li class="{{ active_route('home', 'active', null) }}">
            <a href="{{ route('home') }}">Home</a>
        </li>
        <li class="{{ active_route('contact', 'active', null) }}">
            <a href="{{ route('contact') }}">Contact</a>
        </li>
        <li class="{{ active_route('about', 'active', null) }}">
            <a href="{{ route('about') }}}">About</a>
        </li>
    </ul>
</nav>

str_possessive

此函数将返回您给出的主题字符串的所有格形式

echo str_possessive('Brian') . ' house.'; // "Brian's house."

它只会为以 szch 结尾的主题附加撇号(不带末尾的 s

echo str_possessive('Dolores') . ' eyes.'; // "Dolores' eyes."
echo str_possessive('Sanchez') . ' shoes.'; // "Sanchez' shoes."
echo str_possessive('Gretch') . ' plate.'; // "Gretch' plate."

pipe

pipe() 函数将简单地返回 Laravel 容器中的 \Illuminate\Pipeline\Pipeline 类的实例,这允许进行一些巧妙的链式调用

echo pipe('hello')->through([
    AddComma::class,
    AddWorld::class,
])->then(function ($content) {
    return $content;
}); // This will output "hello, world!"

// AddComma class:
class AddComma
{
    public function handle($string, Closure $next)
    {
        return $next($string . ',');
    }
}

// AddWorld class:
class AddWorld
{
    public function handle($string, Closure $next)
    {
        return $next($string . ' world!');
    }
}

贡献

欢迎所有贡献(拉取请求、问题及功能请求)。但首先请阅读 CONTRIBUTING.md。请参阅 贡献者页面 以了解所有贡献者。

许可证

sven/helpers 采用 MIT 许可证(MIT)授权。有关更多信息,请参阅 许可证文件