laravel already have so many global helper functions
framework already usng so many heplers frameerok itself
array_merge -> one of the helper
helper categories
arrays and objects
paths
strings
fluent strings
URLs
default functions calling as hellpers
https://laravel.com/docs/8.x/helpers#main-content
Arr::collapse()
The Arr::collapse
method collapses an array of arrays into a single array:
use Illuminate\Support\Arr; $array = Arr::collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
array first with function
use Illuminate\Support\Arr; $array = [100, 200, 300]; $first = Arr::first($array, function ($value, $key) { return $value >= 150;}); // 200
Arrays & Objects
Arr::accessibleArr::addArr::collapseArr::crossJoinArr::divideArr::dotArr::exceptArr::existsArr::firstArr::flattenArr::forgetArr::getArr::hasArr::hasAnyArr::isAssocArr::lastArr::onlyArr::pluckArr::prependArr::pullArr::queryArr::randomArr::setArr::shuffleArr::sortArr::sortRecursiveArr::toCssClassesArr::undotArr::whereArr::whereNotNullArr::wrapdata_filldata_getdata_setheadlast
Paths
Strings
__class_basenameepreg_replace_arrayStr::afterStr::afterLastStr::asciiStr::beforeStr::beforeLastStr::betweenStr::camelStr::containsStr::containsAllStr::endsWithStr::finishStr::headlineStr::isStr::isAsciiStr::isUuidStr::kebabStr::lengthStr::limitStr::lowerStr::markdownStr::maskStr::orderedUuidStr::padBothStr::padLeftStr::padRightStr::pluralStr::pluralStudlyStr::randomStr::removeStr::replaceStr::replaceArrayStr::replaceFirstStr::replaceLastStr::reverseStr::singularStr::slugStr::snakeStr::startStr::startsWithStr::studlyStr::substrStr::substrCountStr::substrReplaceStr::titleStr::toHtmlStringStr::ucfirstStr::upperStr::uuidStr::wordCountStr::wordstranstrans_choice
Fluent Strings
The ltrim
method trims the left side of the string:
use Illuminate\Support\Str; $string = Str::of(' Laravel ')->ltrim(); // 'Laravel ' $string = Str::of('/Laravel/')->ltrim('/'); // 'Laravel/'
markdown
The markdown
method converts GitHub flavored Markdown into HTML:
use Illuminate\Support\Str; $html = Str::of('# Laravel')->markdown(); // <h1>Laravel</h1> $html = Str::of('# Taylor <b>Otwell</b>')->markdown([ 'html_input' => 'strip',]); // <h1>Taylor Otwell</h1>
afterafterLastappendasciibasenamebeforebeforeLastbetweencamelcontainscontainsAlldirnameendsWithexactlyexplodefinishisisAsciiisEmptyisNotEmptyisUuidkebablengthlimitlowerltrimmarkdownmaskmatchmatchAllpadBothpadLeftpadRightpipepluralprependremovereplacereplaceArrayreplaceFirstreplaceLastreplaceMatchesrtrimscansingularslugsnakesplitstartstartsWithstudlysubstrsubstrReplacetaptesttitletrimucfirstupperwhenwhenContainswhenContainsAllwhenEmptywhenNotEmptywhenStartsWithwhenEndsWithwhenExactlywhenIswhenIsAsciiwhenIsUuidwhenTestwordCountwords
URLs
action()
The action
function generates a URL for the given controller action:
use App\Http\Controllers\HomeController; $url = action([HomeController::class, 'index']);
If the method accepts route parameters, you may pass them as the second argument to the method:
$url = action([UserController::class, 'profile'], ['id' => 1]);
asset()
The asset
function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS):
$url = asset('img/photo.jpg');
You can configure the asset URL host by setting the ASSET_URL
variable in your .env
file. This can be useful if you host your assets on an external service like Amazon S3 or another CDN:
// ASSET_URL=http://example.com/assets $url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
route()
The route
function generates a URL for a given named route:
$url = route('route.name');
If the route accepts parameters, you may pass them as the second argument to the function:
$url = route('route.name', ['id' => 1]);
By default, the route
function generates an absolute URL. If you wish to generate a relative URL, you may pass false
as the third argument to the function:
$url = route('route.name', ['id' => 1], false);
secure_asset()
The secure_asset
function generates a URL for an asset using HTTPS:
$url = secure_asset('img/photo.jpg');
secure_url()
The secure_url
function generates a fully qualified HTTPS URL to the given path. Additional URL segments may be passed in the function's second argument:
$url = secure_url('user/profile'); $url = secure_url('user/profile', [1]);
url()
The url
function generates a fully qualified URL to the given path:
$url = url('user/profile'); $url = url('user/profile', [1]);
If no path is provided, an Illuminate\Routing\UrlGenerator
instance is returned:
$current = url()->current(); $full = url()->full(); $previous = url()->previous();
Miscellaneous
abort()
The abort
function throws an HTTP exception which will be rendered by the exception handler:
abort(403);
abortabort_ifabort_unlessappauthbackbcryptblankbroadcastcacheclass_uses_recursivecollectconfigcookiecsrf_fieldcsrf_tokendddispatchdumpenveventfilledinfologgermethod_fieldnowoldoptionalpolicyredirectreportrequestrescueresolveresponseretrysessiontapthrow_ifthrow_unlesstodaytrait_uses_recursivetransformvalidatorvalueviewwith
The preg_replace_array
function replaces a given pattern in the string sequentially using an array:
$string = 'The event will take place between :start and :end'; $replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string); // The event will take place between 8:30 and 9:00
Str::afterLast()
The Str::afterLast
method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string:
use Illuminate\Support\Str; $slice = Str::afterLast('App\Http\Controllers\Controller', '\\'); // 'Controller'
Which function returns the application's root directory path?
Marked Answer : base_path()
Correct Answer : base_path()
The _____ function runs PHP's htmlspecialchars function.
Marked Answer : e()
Correct Answer : e()
Choose the correct output for Str::after('This is my name', 'This is');
Marked Answer : " my name"
Correct Answer : " my name"
Which method adds a single instance of the given value to a string if it does not already end with that value?
Marked Answer : --
Correct Answer : Str::finish()
Which function generates an HTML hidden input field containing the value of the CSRF token?
Marked Answer : csrf_token()
Correct Answer : csrf_field()
The ____ method invokes the given closure if a given condition is true.
Marked Answer : contains()
Correct Answer : when()
No comments:
Post a Comment