sven / helpers
A collection of useful helper functions for in Laravel applications.
Requires
- php: ^7.0
- illuminate/support: 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.*
Requires (Dev)
- graham-campbell/testbench: ^3.3
- phpunit/phpunit: ^4.8 || ^5.0
This package is auto-updated.
Last update: 2024-11-05 18:39:10 UTC
README
Helpers
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!
Installation
Via composer:
$ 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:
{ "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.
active_route
This function will return true
if you're on the given route name, false
otherwise:
$isHome = active_route('home');
You may also pass in optional $positive
or $negative
values to return:
$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:
$isContactOrAbout = active_route(['contact', 'about']);
active_route()
can be tremendously useful for active
states on for instance navigation in blade templates:
<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:
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
:
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:
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.
License
sven/helpers
is licensed under the MIT License (MIT). Please see the
license file for more information.