, (*1)
Helpers
![Software License][ico-license]
![Build Status][ico-circleci]
, (*2)
This is a collection of useful helpers for use in Laravel applications. Mainly
made for personal use, but if you find (some of) the helpers useful, feel free
to use it!, (*3)
Installation
Via composer:, (*4)
$ composer require sven/helpers
Or add the package to your dependencies in composer.json and run
composer install on the command line to download the package:, (*5)
{
"require": {
"sven/helpers": "^1.0"
}
}
Available functions
All available functions are listed here, along with their usage and an example on how I would use them., (*6)
active_route
This function will return true if you're on the given route name, false otherwise:, (*7)
$isHome = active_route('home');
You may also pass in optional $positive or $negative values to return:, (*8)
$isContact = active_route('contact', 'Yes!', 'No :(');
There is also an option to give it an array of multiple route names instead of just one. The function will
return $positive if the current route matches any of the given ones, $negative otherwise:, (*9)
$isContactOrAbout = active_route(['contact', 'about']);
active_route() can be tremendously useful for active states on for instance navigation in blade templates:, (*10)
<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
This function will return the possessive form of a subject string you give it:, (*11)
echo str_possessive('Brian') . ' house.'; // "Brian's house."
It will only append an apostrophe (without the trailing s) if the given subject ends
in s, z or ch:, (*12)
echo str_possessive('Dolores') . ' eyes.'; // "Dolores' eyes."
echo str_possessive('Sanchez') . ' shoes.'; // "Sanchez' shoes."
echo str_possessive('Gretch') . ' plate.'; // "Gretch' plate."
pipe
The pipe() function will simply return an instance of the \Illuminate\Pipeline\Pipeline
class from Laravel's container, which allows for some neat chaining:, (*13)
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
All contributions (pull requests, issues and feature requests) are
welcome. Make sure to read through the CONTRIBUTING.md first,
though. See the contributors page for all contributors., (*14)
License
sven/helpers is licensed under the MIT License (MIT). Please see the
license file for more information., (*15)