助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
* * * * * [TOC] ## 简介 Laravel 包含有各种各样的 PHP 辅助函数,许多都是在 Laravel 自身框架中使用到。如果你觉得实用,也可以在你自己的应用中使用它们。 ## 可用方法 ### 数组 [array_add](#array_add_108) [array_collapse](#array_collapse_118) [array_divide](#array_divide_128) [array_dot](#array_dot_140) [array_except](#array_except_150) [array_first](#array_first_162) [array_flatten](#array_flatten_182) [array_forget](#array_forget_194) [array_get](#array_get_206) [array_has](#array_has_224) [array_last](#array_last_240) [array_only](#array_only_254) [array_pluck](#array_pluck_266) [array_prepend](#array_prepend_289) [array_pull](#array_pull_301) [array_set](#array_set_315) [array_sort](#array_sort_327) [array_sort_recursive](#array_sort_recursive_349) [array_where](#array_where_385) [head](#head_399) [last](#last_411) ### 路径 [app_path](#app_path_425) [base_path](#base_path_435) [config_path](#config_path_445) [database_path](#database_path_453) [mix](#mix_461) [public_path](#public_path_469) [resource_path](#resource_path_477) [storage_path](#storage_path_487) ### 字符串 [camel_case](#camel_case_499) [class_basename](#class_basename_509) [e](#e_519) [ends_with](#ends_with_529) [snake_case](#snake_case_539) [str_limit](#str_limit_549) [starts_with](#starts_with_559) [str_contains](#str_contains_569) [str_finish](#str_finish_587) [str_is](#str_is_597) [str_plural](#str_plural_611) [str_random](#str_random_637) [str_singular](#str_singular_645) [str_slug](#str_slug_655) [studly_case](#studly_case_665) [title_case](#title_case_675) [trans](#trans_685) [trans_choice](#trans_choice_693) ### URLs [action](#action_703) [asset](#asset_717) [secure_asset](#secure_asset_725) [route](#route_733) [secure_url](#secure_url_747) [url](#url_757) ### 其他 [abort](#abort_777) [abort_if](#abort_if_791) [abort_unless](#abort_unless_799) [auth](#auth_807) [back](#back_815) [bcrypt](#bcrypt_823) [cache](#cache_831) [collect](#collect_849) [config](#config_857) [csrf_field](#csrf_field_873) [csrf_token](#csrf_token_881) [dd](#dd_889) [dispatch](#dispatch_905) [env](#env_913) [event](#event_924) [factory](#factory_932) [info](#info_940) [logger](#logger_954) [method_field](#method_field_974) [old](#old_984) [redirect](#redirect_994) [request](#request_1004) [response](#response_1014) [retry](#retry_1024) [session](#session_1034) [value](#value_1056) [view](#view_1064) ## 方法列表 ## 数组 #### `array_add()` 如果给定的键不存在与数组中,`array_add` 就会把给定的键值对添加到数组中: ~~~ $array = array_add(['name' => 'Desk'], 'price', 100); // ['name' => 'Desk', 'price' => 100] ~~~ #### `array_collapse()` `array_collapse` 函数把数组里的每一个数组合并成单个数组: ~~~ $array = array_collapse([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); // [1, 2, 3, 4, 5, 6, 7, 8, 9] ~~~ #### `array_divide()` `array_divide` 函数返回两个数组,一个包含原本数组的键,另一个包含原本数组的值: ~~~ list($keys, $values) = array_divide(['name' => 'Desk']); // $keys: ['name'] // $values: ['Desk'] ~~~ #### `array_dot()` `array_dot` 函数把多维数组压制成一维数组,并用「点」式语法表示深度: ~~~ $array = array_dot(['foo' => ['bar' => 'baz']]); // ['foo.bar' => 'baz']; ~~~ #### `array_except()` `array_except` 函数从数组移除指定的键值对: ~~~ $array = ['name' => 'Desk', 'price' => 100]; $array = array_except($array, ['price']); // ['name' => 'Desk'] ~~~ #### `array_first()` `array_first` 函数返回数组中第一个通过指定测试的元素: ~~~ $array = [100, 200, 300]; $value = array_first($array, function ($value, $key) { return $value >= 150; }); // 200 ~~~ 可传递第三个参数作为默认值。当没有元素通过测试时,将会返回该默认值: ~~~ $value = array_first($array, $callback, $default); ~~~ #### `array_flatten()` `array_flatten` 函数将多维数组压制成一维数组: ~~~ $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $array = array_flatten($array); // ['Joe', 'PHP', 'Ruby']; ~~~ #### `array_forget()` `array_forget` 函数以「点」式语法从深度嵌套的数组中移除指定的键值对: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; array_forget($array, 'products.desk'); // ['products' => []] ~~~ #### `array_get()` `array_get` 函数使用「点」式语法从深度嵌套的数组中获取指定的值: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; $value = array_get($array, 'products.desk'); // ['price' => 100] ~~~ `array_get` 函数同样也接受默认值,如果指定的键找不到时,则返回该默认值: ~~~ $value = array_get($array, 'names.john', 'default'); ~~~ #### `array_has()` `array_has` 函数使用「点」式语法检查指定的项目是否存在于数组中: ~~~ $array = ['product' => ['name' => 'desk', 'price' => 100]]; $hasItem = array_has($array, 'product.name'); // true $hasItems = array_has($array, ['product.price', 'product.discount']); // false ~~~ #### `array_last()` `array_last` 函数返回数组中最后一个通过指定测试的元素: ~~~ $array = [100, 200, 300, 110]; $value = array_last($array, function ($value, $key) { return $value >= 150; }); // 300 ~~~ #### `array_only()` `array_only` 函数从数组返回指定的键值对: ~~~ $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10]; $array = array_only($array, ['name', 'price']); // ['name' => 'Desk', 'price' => 100] ~~~ #### `array_pluck()` `array_pluck` 函数从数组拉出一列指定的键值对: ~~~ $array = [ ['developer' => ['id' => 1, 'name' => 'Taylor']], ['developer' => ['id' => 2, 'name' => 'Abigail']], ]; $array = array_pluck($array, 'developer.name'); // ['Taylor', 'Abigail']; ~~~ 你也可以指定要以什么作为结果列的键名: ~~~ $array = array_pluck($array, 'developer.name', 'developer.id'); // [1 => 'Taylor', 2 => 'Abigail']; ~~~ #### `array_prepend()` `array_prepend` 函数将元素加到数组的头部: ~~~ $array = ['one', 'two', 'three', 'four']; $array = array_prepend($array, 'zero'); // $array: ['zero', 'one', 'two', 'three', 'four'] ~~~ #### `array_pull()` `array_pull` 函数从数组移除指定键值对并返回该键值对: ~~~ $array = ['name' => 'Desk', 'price' => 100]; $name = array_pull($array, 'name'); // $name: Desk // $array: ['price' => 100] ~~~ #### `array_set()` `array_set` 函数使用「点」式语法在深度嵌套的数组中写入值: ~~~ $array = ['products' => ['desk' => ['price' => 100]]]; array_set($array, 'products.desk.price', 200); // ['products' => ['desk' => ['price' => 200]]] ~~~ #### `array_sort()` `array_sort` 函数根据指定闭包的结果排序数组: ~~~ $array = [ ['name' => 'Desk'], ['name' => 'Chair'], ]; $array = array_values(array_sort($array, function ($value) { return $value['name']; })); /* [ ['name' => 'Chair'], ['name' => 'Desk'], ] */ ~~~ #### `array_sort_recursive()` `array_sort_recursive` 函数使用 sort 函数递归排序数组: ~~~ $array = [ [ 'Roman', 'Taylor', 'Li', ], [ 'PHP', 'Ruby', 'JavaScript', ], ]; $array = array_sort_recursive($array); /* [ [ 'Li', 'Roman', 'Taylor', ], [ 'JavaScript', 'PHP', 'Ruby', ] ]; */ ~~~ #### `array_where()` `array_where` 函数使用指定的闭包过滤数组: ~~~ $array = [100, '200', 300, '400', 500]; $array = array_where($array, function ($value, $key) { return is_string($value); }); // [1 => 200, 3 => 400] ~~~ #### `head()` `head` 函数返回指定数组的第一个元素: ~~~ $array = [100, 200, 300]; $first = head($array); // 100 ~~~ #### `last()` `last` 函数返回指定数组的最后一个元素: ~~~ $array = [100, 200, 300]; $last = last($array); // 300 ~~~ ## 路径 #### `app_path()` `app_path` 函数返回 `app` 文件夹的完整路径。你也可以使用 `app_path` 函数生成针对指定文件相对于 app 目录的完整路径: ~~~ $path = app_path(); $path = app_path('Http/Controllers/Controller.php'); ~~~ #### `base_path()` `base_path` 函数返回项目根目录的完整路径。你也可以使用 `base_path` 函数生成针对指定文件相对于项目根目录的完整路径: ~~~ $path = base_path(); $path = base_path('vendor/bin'); ~~~ #### `config_path()` `config_path` 函数返回 `config` 目录的完整路径: ~~~ $path = config_path(); ~~~ #### `database_path()` `database_path` 函数返回 `database` 目录的完整路径: ~~~ $path = database_path(); ~~~ #### `mix()` `mix` 函数获取带有版本号的 [mix](https://laravel-china.org/docs/laravel/5.4/mix) 文件: ~~~ mix($file); ~~~ #### `public_path()` `public_path` 函数返回 `public` 目录的完整路径: ~~~ $path = public_path(); ~~~ #### `resource_path()` `resource_path` 函数返回 `resources` 目录的完整路径。你也可以使用 `resource_path` 函数生成针对指定文件相对于 `resources` 目录的完整路径: ~~~ $path = resource_path(); $path = resource_path('assets/sass/app.scss'); ~~~ #### `storage_path()` `storage_path` 函数返回 `storage` 目录的完整路径。你也可以使用 `storage_path` 函数生成针对指定文件相对于 `storage` 目录的完整路径: ~~~ $path = storage_path(); $path = storage_path('app/file.txt'); ~~~ ## 字符串 #### `camel_case()` `camel_case` 函数将指定的字符串转换成 `驼峰式命名`: ~~~ $camel = camel_case('foo_bar'); // fooBar ~~~ #### `class_basename()` `class_basename` 函数返回不包含命名空间的类名称: ~~~ $class = class_basename('Foo\Bar\Baz'); // Baz ~~~ #### `e()` `e` 函数对指定字符串进行 `htmlentities`: ~~~ echo e('<html>foo</html>'); // &lt;html&gt;foo&lt;/html&gt; ~~~ #### `ends_with()` `ends_with` 函数判断指定字符串结尾是否为指定内容: ~~~ $value = ends_with('This is my name', 'name'); // true ~~~ #### `snake_case()` `snake_case` 函数将指定的字符串转换成 `蛇形命名` : ~~~ $snake = snake_case('fooBar'); // foo_bar ~~~ #### `str_limit()` `str_limit` 函数限制字符串的字符个数,该函数接受一个字符串作为第一个参数,第二个参数为允许的最大字符个数: ~~~ $value = str_limit('The PHP framework for web artisans.', 7); // The PHP... ~~~ #### `starts_with()` `starts_with` 函数判断字符串开头是否为指定内容: ~~~ $value = starts_with('This is my name', 'This'); // true ~~~ #### `str_contains()` `str_contains` 函数判断字符串是否包含有指定内容: ~~~ $value = str_contains('This is my name', 'my'); // true ~~~ 你也可以传递数组,来判断字符串是否包任意指定内容: ~~~ $value = str_contains('This is my name', ['my', 'foo']); // true ~~~ #### `str_finish()` `str_finish` 函数添加指定内容到字符串末尾: ~~~ $string = str_finish('this/string', '/'); // this/string/ ~~~ #### `str_is()` `str_is` 函数判断指定的字符串是否匹配指定的格式,星号可作为通配符使用: ~~~ $value = str_is('foo*', 'foobar'); // true $value = str_is('baz*', 'foobar'); // false ~~~ #### `str_plural()` `str_plural` 函数把字符串转换成复数形式。该函数目前只支持英文: ~~~ $plural = str_plural('car'); // cars $plural = str_plural('child'); // children ~~~ 你可以传入一个整数作为第二个参数,来获取字符串的单数或复数形式: ~~~ $plural = str_plural('child', 2); // children $plural = str_plural('child', 1); // child ~~~ #### `str_random()` `str_random` 函数生成指定长度的随机字符串。该函数使用了 PHP 自带的 `random_bytes` 函数: ~~~ $string = str_random(40); ~~~ #### `str_singular()` `str_singular` 函数把字符串转换成单数形式。该函数目前只支持英文: ~~~ $singular = str_singular('cars'); // car ~~~ #### `str_slug()` `str_slug` 函数根据指定字符串生成 URL 友好的「slug」: ~~~ $title = str_slug('Laravel 5 Framework', '-'); // laravel-5-framework ~~~ #### `studly_case()` `studly_case` 函数把指定字符串转换成 `首字母大写`: ~~~ $value = studly_case('foo_bar'); // FooBar ~~~ #### `title_case()` `title_case` 函数把指定字符串转换成 `每个单词首字母大写`: ~~~ $title = title_case('a nice title uses the correct case'); // A Nice Title Uses The Correct Case ~~~ #### `trans()` `trans` 函数根据你的 [本地化文件](https://laravel-china.org/docs/laravel/5.4/localization) 翻译指定的语句: ~~~ echo trans('validation.required'): ~~~ #### `trans_choice()` `trans_choice` 函数根据给定数量来决定翻译指定语句是复数形式还是单数形式: ~~~ $value = trans_choice('foo.bar', $count); ~~~ ## URLs #### `action()` `action` 函数根据指定控制器的方法生成 URL,你不需要传入该控制器的完整命名空间。只需要传入相对于 `App\Http\Controllers` 命名空间的控制器类名: ~~~ $url = action('HomeController@getIndex'); ~~~ 如果该方法接受路由参数,可以作为第二个参数传入: ~~~ $url = action('UserController@profile', ['id' => 1]); ~~~ #### `asset()` 根据当前请求的协议(HTTP 或 HTTPS)生成资源文件的 URL: ~~~ $url = asset('img/photo.jpg'); ~~~ #### `secure_asset()` 使用 HTTPS 协议生成资源文件的 URL: ~~~ echo secure_asset('foo/bar.zip', $title, $attributes = []); ~~~ #### `route()` `route` 函数生成指定路由名称的 URL: ~~~ $url = route('routeName'); ~~~ 如果该路由接受参数,可以作为第二个参数传入: ~~~ $url = route('routeName', ['id' => 1]); ~~~ #### `secure_url()` `secure_url` 数使用 HTTPS 协议生成指定路径的完整 URL: ~~~ echo secure_url('user/profile'); echo secure_url('user/profile', [1]); ~~~ #### `url()` `url` 函数生成指定路径的完整 URL: ~~~ echo url('user/profile'); echo url('user/profile', [1]); ~~~ 如果没有提供路径参数,将会返回一个 `Illuminate\Routing\UrlGenerator` 实例: ~~~ echo url()->current(); echo url()->full(); echo url()->previous(); ~~~ ## 其他 #### `abort()` `abort` 函数抛出一个将被异常处理句柄渲染的 HTTP 异常: ~~~ abort(401); ~~~ 你也可以传入异常的响应消息: ~~~ abort(401, 'Unauthorized.'); ~~~ #### `abort_if()` `abort_if` 函数如果指定的布尔表达式值为 `true` 则抛出一个 HTTP 异常: ~~~ abort_if(! Auth::user()->isAdmin(), 403); ~~~ #### `abort_unless()` `abort_unless` 函数如果指定的布尔表达式值为 `false` 则抛出一个 HTTP 异常: ~~~ abort_unless(Auth::user()->isAdmin(), 403); ~~~ #### `auth()` `auth` 函数返回一个 authenticator 实例,可以使用它来代替 Auth facade: ~~~ $user = auth()->user(); ~~~ #### `back()` `back()` 函数生成一个重定向响应让用户返回到之前的位置: ~~~ return back(); ~~~ #### `bcrypt()` `bcrypt` 函数使用 Bcrypt 算法哈希指定的数值。你可以使用它代替 `Hash` facade: ~~~ $password = bcrypt('my-secret-password'); ~~~ #### `cache()` `cache` 函数尝试从缓存获取给定 `key` 的值。如果 `key` 不存在则返回默认值: ~~~ $value = cache('key'); $value = cache('key', 'default'); ~~~ 同时,你也可以传递键值对来设置缓存,第二个参数可以指定缓存的过期时间,单位分钟: ~~~ cache(['key' => 'value'], 5); cache(['key' => 'value'], Carbon::now()->addSeconds(10)); ~~~ #### `collect()` `collect` 函数根据指定的数组生成 [集合](https://laravel-china.org/docs/laravel/5.4/collections) 实例: ~~~ $collection = collect(['taylor', 'abigail']); ~~~ #### `config()` `config` 函数用于获取配置信息的值,配置信息的值可通过「点」式语法访问,其中包含要访问的文件名以及选项名。可传递一个默认值作为第二参数,当配置信息不存在时,则返回该默认值: ~~~ $value = config('app.timezone'); $value = config('app.timezone', $default); ~~~ `config` 辅助函数也可以在运行期间,根据指定的键值对设置指定的配置信息: ~~~ config(['app.debug' => true]); ~~~ #### `csrf_field()` `csrf_field` 函数生成包含 CSRF 令牌内容的 HTML 表单隐藏字段。例如,使用 [Blade 语法](https://laravel-china.org/docs/laravel/5.4/blade): ~~~ {{ csrf_field() }} ~~~ #### `csrf_token()` `csrf_token` 函数获取当前 CSRF 令牌的内容: ~~~ $token = csrf_token(); ~~~ #### `dd()` `dd` 函数输出指定变量的值并终止脚本运行: ~~~ dd($value); dd($value1, $value2, $value3, ...); ~~~ 如果你不想终止脚本运行,使用 `dump` 函数代替: ~~~ dump($value); ~~~ #### `dispatch()` `dispatch` 函数把一个新任务推送到 Laravel 的 [任务队列](https://laravel-china.org/docs/laravel/5.4/queues)中: ~~~ dispatch(new App\Jobs\SendEmails); ~~~ #### `env()` `env` 函数获取环境变量值或返回默认值: ~~~ $env = env('APP_ENV'); // Return a default value if the variable doesn't exist... $env = env('APP_ENV', 'production'); ~~~ #### `event()` `event` 函数派发指定的 [事件](https://laravel-china.org/docs/laravel/5.4/events) 到所属的侦听器: ~~~ event(new UserRegistered($user)); ~~~ #### `factory()` `factory` 函数根据指定类、名称以及数量生成模型工厂构造器(model factory builder)。可用于 [测试](https://laravel-china.org/docs/laravel/5.4/database-testing#writing-factories) 或 [数据填充](https://laravel-china.org/docs/laravel/5.4/seeding#using-model-factories): ~~~ $user = factory(App\User::class)->make(); ~~~ #### `info()` `info` 函数以 `info` 级别写入日志: ~~~ info('Some helpful information!'); ~~~ 包含上下文数据的数组可以通过第二个参数传递给函数: ~~~ info('User login attempt failed.', ['id' => $user->id]); ~~~ #### `logger()` `logger` 函数以 `debug` 级别写入日志: ~~~ logger('Debug message'); ~~~ 同时支持传入数组作为参数: ~~~ logger('User has logged in.', ['id' => $user->id]); ~~~ 如果没有传入参数,则会返回一个 [日志](https://laravel-china.org/docs/laravel/5.4/errors#logging) 的实例: ~~~ logger()->error('You are not allowed here.'); ~~~ #### `method_field()` `method_field` 函数生成模拟各种 HTTP 动作请求的 HTML 表单隐藏字段。例如,使用 [Blade 语法](https://laravel-china.org/docs/laravel/5.4/blade): ~~~ <form method="POST"> {{ method_field('DELETE') }} </form> ~~~ #### `old()` `old` 函数 [获取](https://laravel-china.org/docs/laravel/5.4/requests#retrieving-input) session 内一次性的历史输入值: ~~~ $value = old('value'); $value = old('value', 'default'); ~~~ #### `redirect()` `redirect` 函数返回一个 HTTP 重定向响应,如果调用时没有传入参数则返回 redirector 实例: ~~~ return redirect('/home'); return redirect()->route('route.name'); ~~~ #### `request()` `request` 函数返回当前 [请求](https://laravel-china.org/docs/laravel/5.4/requests) 实例或获取输入的项目: ~~~ $request = request(); $value = request('key', $default = null) ~~~ #### `response()` `response` 函数创建一个 [响应](https://laravel-china.org/docs/laravel/5.4/responses) 实例或获取一个 response 工厂实例: ~~~ return response('Hello World', 200, $headers); return response()->json(['foo' => 'bar'], 200, $headers); ~~~ #### `retry()` `retry` 函数将会重复调用给定的回调函数,最多调用指定的次数。如果回调函数没有抛出异常并且有值返回,则 `retry` 函数返回该值。如果回调函数抛出异常,`retry` 函数将拦截异常并自动再次调用回调函数,直到调用给定的次数。如果重试次数超出给定次数,拦截的异常将会抛出: ~~~ return retry(5, function () { // Attempt 5 times while resting 100ms in between attempts... }, 100); ~~~ #### `session()` `session` 函数可用于获取或设置单个 session 项: ~~~ $value = session('key'); ~~~ 你可以通过传递键值对数组给该函数设置 session 项: ~~~ session(['chairs' => 7, 'instruments' => 3]); ~~~ 该函数在没有传递参数时,将返回 session 实例: ~~~ $value = session()->get('key'); session()->put('key', $value); ~~~ #### `value()` `value` 函数返回指定数值。而当你传递一个 `闭包` 给该函数时,该 `闭包` 将被运行并返回该 `闭包` 的运行结果: ~~~ $value = value(function() { return 'bar'; }); ~~~ #### `view()` `view` 函数获取 [视图](https://laravel-china.org/docs/laravel/5.4/views) 实例: ~~~ return view('auth.login'); ~~~