|
| 1 | +# Helper Functions |
| 2 | + |
| 3 | +- [Arrays](#arrays) |
| 4 | +- [Paths](#paths) |
| 5 | +- [Strings](#strings) |
| 6 | +- [URLs](#urls) |
| 7 | +- [Miscellaneous](#miscellaneous) |
| 8 | + |
| 9 | +<a name="arrays"></a> |
| 10 | +## Arrays |
| 11 | + |
| 12 | +### array_add |
| 13 | + |
| 14 | +The `array_add` function adds a given key / value pair to the array if the given key doesn't already exist in the array. |
| 15 | + |
| 16 | + $array = array('foo' => 'bar'); |
| 17 | + |
| 18 | + $array = array_add($array, 'key', 'value'); |
| 19 | + |
| 20 | +### array_divide |
| 21 | + |
| 22 | +The `array_divide` function returns two arrays, one containing the keys, and the other containing the values of the original array. |
| 23 | + |
| 24 | + $array = array('foo' => 'bar'); |
| 25 | + |
| 26 | + list($keys, $values) = array_divide($array); |
| 27 | + |
| 28 | +### array_dot |
| 29 | + |
| 30 | +The `array_dot` function flattens a multi-dimensional array into a single level array that uses "dot" notation to indicate depth. |
| 31 | + |
| 32 | + $array = array('foo' => array('bar' => 'baz')); |
| 33 | + |
| 34 | + $array = array_dot($array); |
| 35 | + |
| 36 | + // array('foo.bar' => 'baz'); |
| 37 | + |
| 38 | +### array_except |
| 39 | + |
| 40 | +The `array_except` method removes the given key / value pairs from the array. |
| 41 | + |
| 42 | + $array = array_except($array, array('keys', 'to', 'remove')); |
| 43 | + |
| 44 | +### array_fetch |
| 45 | + |
| 46 | +The `array_fetch` method returns a flattened array containing the selected nested element. |
| 47 | + |
| 48 | + $array = array(array('name' => 'Taylor'), array('name' => 'Dayle')); |
| 49 | + |
| 50 | + var_dump(array_fetch($array, 'name')); |
| 51 | + |
| 52 | + // array('Taylor', 'Dayle'); |
| 53 | + |
| 54 | +### array_first |
| 55 | + |
| 56 | +The `array_first` method returns the first element of an array passing a given truth test. |
| 57 | + |
| 58 | + $array = array(100, 200, 300); |
| 59 | + |
| 60 | + $value = array_first($array, function($key, $value) |
| 61 | + { |
| 62 | + return $value >= 150; |
| 63 | + }); |
| 64 | + |
| 65 | +A default value may also be passed as the third parameter: |
| 66 | + |
| 67 | + $value = array_first($array, $callback, $default); |
| 68 | + |
| 69 | +### array_flatten |
| 70 | + |
| 71 | +The `array_flatten` method will flatten a multi-dimensional array into a single level. |
| 72 | + |
| 73 | + $array = array('name' => 'Joe', 'languages' => array('PHP', 'Ruby')); |
| 74 | + |
| 75 | + $array = array_flatten($array); |
| 76 | + |
| 77 | + // array('Joe', 'PHP', 'Ruby'); |
| 78 | + |
| 79 | +### array_forget |
| 80 | + |
| 81 | +The `array_forget` method will remove a given key / value pair from a deeply nested array using "dot" notation. |
| 82 | + |
| 83 | + $array = array('names' => array('joe' => array('programmer'))); |
| 84 | + |
| 85 | + $array = array_forget($array, 'names.joe'); |
| 86 | + |
| 87 | +### array_get |
| 88 | + |
| 89 | +The `array_get` method will retrieve a given value from a deeply nested array using "dot" notation. |
| 90 | + |
| 91 | + $array = array('names' => array('joe' => array('programmer'))); |
| 92 | + |
| 93 | + $value = array_get($array, 'names.joe'); |
| 94 | + |
| 95 | +### array_only |
| 96 | + |
| 97 | +The `array_only` method will return only the specified key / value pairs from the array. |
| 98 | + |
| 99 | + $array = array('name' => 'Joe', 'age' => 27, 'votes' => 1); |
| 100 | + |
| 101 | + $array = array_only($array, array('name', 'votes')); |
| 102 | + |
| 103 | +### array_pluck |
| 104 | + |
| 105 | +The `array_pluck` method will pluck a list of the given key / value pairs from the array. |
| 106 | + |
| 107 | + $array = array(array('name' => 'Taylor'), array('name' => 'Dayle')); |
| 108 | + |
| 109 | + $array = array_pluck($array, 'name'); |
| 110 | + |
| 111 | + // array('Taylor', 'Dayle'); |
| 112 | + |
| 113 | +### array_pull |
| 114 | + |
| 115 | +The `array_pull` method will return a given key / value pair from the array, as well as remove it. |
| 116 | + |
| 117 | + $array = array('name' => 'Taylor', 'age' => 27); |
| 118 | + |
| 119 | + $name = array_pluck($array, 'name'); |
| 120 | + |
| 121 | +### array_set |
| 122 | + |
| 123 | +The `array_set` method will set a value within a deeply nested array using "dot" notation. |
| 124 | + |
| 125 | + $array = array('names' => array('programmer' => 'Joe')); |
| 126 | + |
| 127 | + array_set($array, 'names.editor', 'Taylor'); |
| 128 | + |
| 129 | +### head |
| 130 | + |
| 131 | +Return the first element in the array. Useful for method chaining in PHP 5.3.x. |
| 132 | + |
| 133 | + $first = head($this->returnsArray('foo')); |
| 134 | + |
| 135 | +### last |
| 136 | + |
| 137 | +Return the last element in the array. Useful for method chaining. |
| 138 | + |
| 139 | + $last = last($this->returnsArray('foo')); |
| 140 | + |
| 141 | +<a name="paths"></a> |
| 142 | +## Paths |
| 143 | + |
| 144 | +### app_path |
| 145 | + |
| 146 | +Get the fully qualified path to the `application` directory. |
| 147 | + |
| 148 | +### base_path |
| 149 | + |
| 150 | +Get the fully qualified path to the root of the application install. |
| 151 | + |
| 152 | +### public_path |
| 153 | + |
| 154 | +Get the fully qualified path to the `public` directory. |
| 155 | + |
| 156 | +### storage_path |
| 157 | + |
| 158 | +Get the fully qualified path to the `application/storage` directory. |
| 159 | + |
| 160 | +<a name="strings"></a> |
| 161 | +## Strings |
| 162 | + |
| 163 | +### camel_case |
| 164 | + |
| 165 | +Convert the given string to `camelCase`. |
| 166 | + |
| 167 | + $camel = camsel_case('foo_bar'); |
| 168 | + |
| 169 | + // fooBar |
| 170 | + |
| 171 | +### class_basename |
| 172 | + |
| 173 | +Get the class name of the given class, without any namespace names. |
| 174 | + |
| 175 | + $class = class_basename('Foo\Bar\Baz'); |
| 176 | + |
| 177 | + // Baz |
| 178 | + |
| 179 | +### e |
| 180 | + |
| 181 | +Run `htmlentites` over the given string, with UTF-8 support. |
| 182 | + |
| 183 | + $entities = e('<html>foo</html>'); |
| 184 | + |
| 185 | +### ends_with |
| 186 | + |
| 187 | +Determine if the given haystack ends with a given needle. |
| 188 | + |
| 189 | + $value = ends_with('This is my name', 'name'); |
| 190 | + |
| 191 | +### snake_case |
| 192 | + |
| 193 | +Convert the given string to `snake_case`. |
| 194 | + |
| 195 | + $snake = snake_case('fooBar'); |
| 196 | + |
| 197 | + // foo_bar |
| 198 | + |
| 199 | +### starts_with |
| 200 | + |
| 201 | +Determine if the given haystack begins with the given needle. |
| 202 | + |
| 203 | + $value = starts_with('This is my name', 'This'); |
| 204 | + |
| 205 | +### str_contains |
| 206 | + |
| 207 | +Determine if the given haystack contains the given needle. |
| 208 | + |
| 209 | + $value = str_contains('This is my name', 'my'); |
| 210 | + |
| 211 | +### str_finish |
| 212 | + |
| 213 | +Add a single instance of the given needle to the haystack. Remove any extra instances. |
| 214 | + |
| 215 | + $string = str_finish('this/string', '/'); |
| 216 | + |
| 217 | + // this/string/ |
| 218 | + |
| 219 | +### str_is |
| 220 | + |
| 221 | +Determine if a given string matches a given pattern. Asterisks may be used to indicate wildcards. |
| 222 | + |
| 223 | + $value = str_is('foo*', 'foobar'); |
| 224 | + |
| 225 | +### str_plural |
| 226 | + |
| 227 | +Convert a string to its plural form (English only). |
| 228 | + |
| 229 | + $plural = str_plural('car'); |
| 230 | + |
| 231 | +### str_random |
| 232 | + |
| 233 | +Generate a random string of the given length. |
| 234 | + |
| 235 | + $string = str_random(40); |
| 236 | + |
| 237 | +### str_singular |
| 238 | + |
| 239 | +Convert a string to its singular form (English only). |
| 240 | + |
| 241 | + $singular = str_singular('cars'); |
| 242 | + |
| 243 | +### studly_case |
| 244 | + |
| 245 | +Convert the given string to `StudlyCase`. |
| 246 | + |
| 247 | + $value = studly_case('foo_bar'); |
| 248 | + |
| 249 | + // FooBar |
| 250 | + |
| 251 | +### trans |
| 252 | + |
| 253 | +Translate a given language line. Alias of `Lang::get`. |
| 254 | + |
| 255 | + $value = trans('validation.required'): |
| 256 | + |
| 257 | +### trans_choice |
| 258 | + |
| 259 | +Tranlate a given language line with inflection. Alias of `Lang::choice`. |
| 260 | + |
| 261 | + $value = trans_choice('foo.bar', $count); |
| 262 | + |
| 263 | +<a name="urls"></a> |
| 264 | +## URLs |
| 265 | + |
| 266 | +### action |
| 267 | + |
| 268 | +Generate a URL for a given controller action. |
| 269 | + |
| 270 | + $url = action('HomeController@getIndex', $params); |
| 271 | + |
| 272 | +### asset |
| 273 | + |
| 274 | +Generate a URL for an asset. |
| 275 | + |
| 276 | + $url = asset('img/photo.jpg'); |
| 277 | + |
| 278 | +### link_to |
| 279 | + |
| 280 | +Generate a HTML link to the given URL. |
| 281 | + |
| 282 | + echo link_to('foo/bar', $title, $attributes = array(), $secure = null); |
| 283 | + |
| 284 | +### link_to_asset |
| 285 | + |
| 286 | +Generate a HTML link to the given asset. |
| 287 | + |
| 288 | + echo link_to_asset('foo/bar.zip', $title, $attributes = array(), $secure = null); |
| 289 | + |
| 290 | +### link_to_route |
| 291 | + |
| 292 | +Generate a HTML link to the given route. |
| 293 | + |
| 294 | + echo link_to_route('route.name', $title, $parameters = array(), $attributes = array()); |
| 295 | + |
| 296 | +### link_to_action |
| 297 | + |
| 298 | +Generate a HTML link to the given controller action. |
| 299 | + |
| 300 | + echo link_to_action('HomeController@getIndex', $title, $parameters = array(), $attributes = array()); |
| 301 | + |
| 302 | +### secure_asset |
| 303 | + |
| 304 | +Generate a HTML link to the given asset using HTTPS. |
| 305 | + |
| 306 | + echo secure_asset('foo/bar.zip', $title, $attributes = array()); |
| 307 | + |
| 308 | +### secure_url |
| 309 | + |
| 310 | +Generate a fully qualified URL to a given path using HTTPS. |
| 311 | + |
| 312 | + echo secure_url('foo/bar', $parameters = array()); |
| 313 | + |
| 314 | +### url |
| 315 | + |
| 316 | +Generate a fully qualified URL to the given path. |
| 317 | + |
| 318 | + echo url('foo/bar', $parameters = array(), $secure = null); |
| 319 | + |
| 320 | +<a name="miscellaneous"></a> |
| 321 | +## Miscellaneous |
| 322 | + |
| 323 | +### csrf_token |
| 324 | + |
| 325 | +Get the value of the current CSRF token. |
| 326 | + |
| 327 | + $token = csrf_token(); |
| 328 | + |
| 329 | +### dd |
| 330 | + |
| 331 | +Dump the given variable and end execution of the script. |
| 332 | + |
| 333 | + dd($value); |
| 334 | + |
| 335 | +### value |
| 336 | + |
| 337 | +If the given value is a `Closure`, return the value returned by the `Closure`. Otherwise, return the value. |
| 338 | + |
| 339 | + $value = value(function() { return 'bar'; }); |
| 340 | + |
| 341 | +### with |
| 342 | + |
| 343 | +Return the given object. Useful for method chaining constructors in PHP 5.3.x. |
| 344 | + |
| 345 | + $value = with(new Foo)->doWork(); |
0 commit comments