Helpers
Winter 包括各种“助手”PHP 函数。其中许多功能由 Winter 本身在内部使用,但是,如果您觉得它们有用,您可以在自己的插件和应用程序中自由使用它们。
Arrays
拉维Arr::*()
帮手
array_add
array_divide
array_dot
array_undot
array_except
array_first
array_flatten
array_forget
array_get
array_only
array_pluck
array_pull
array_set
array_sort
array_sort_recursive
array_where
head
last
Paths
路径符号 app_path base_path config_path database_path media_path plugins_path public_path storage_path temp_path themes_path uploads_path
Strings
拉维Str::*()
帮手
camel_case
class_basename
e
ends_with
snake_case
str_limit
starts_with
str_contains
str_finish
str_is
str_plural
str_random
str_singular
str_slug
studly_case
trans
trans_choice
SVG
Miscellaneous
asset config dd env get input post redirect request response route secure_asset trace_log trace_sql url
Arrays
array_add()
这array_add
如果给定的键不存在于数组中,函数将给定的键/值对添加到数组中:
$array = array_add(['name' => 'Desk'], 'price', 100);
// ['name' => 'Desk', 'price' => 100]
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_undot()
这array_undot
功能是对应的部分array_dot
方法。它将点标记数组转换为标准关联数组:
$array = array_undot([
'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 ($key, $value) {
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_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' => ['name' => 'Brian']],
['developer' => ['name' => 'Stewie']]
];
$array = array_pluck($array, 'developer.name');
// ['Brian', 'Stewie'];
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 = [
[
'Brian',
'Shannon',
'Alec',
],
[
'PHP',
'Ruby',
'JavaScript',
],
];
$array = array_sort_recursive($array);
/*
[
[
'Alec',
'Brian',
'Shannon',
],
[
'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
Paths
路径符号
路径前缀符号可用于创建动态路径。例如,一条路径以~/
将创建一个相对于应用程序的路径:
list: ~/plugins/acme/pay/models/invoiceitem/columns.yaml
这些符号支持创建动态路径:
Symbol | Description |
---|---|
$ |
相对于插件目录 |
~ |
相对于应用目录 |
app_path()
这app_path
函数返回到的完全限定路径app
目录:
$path = app_path();
您也可以使用app_path
函数生成相对于应用程序目录的给定文件的完全限定路径:
$path = app_path('Http/Controllers/Controller.php');
base_path()
这base_path
函数返回项目根目录的完全限定路径:
$path = base_path();
您也可以使用base_path
函数生成相对于应用程序目录的给定文件的完全限定路径:
$path = base_path('vendor/bin');
config_path($path = '')
这config_path
函数返回应用程序配置目录的完全限定路径:
$path = config_path();
您也可以使用config_path
函数生成相对于配置目录的给定文件的完全限定路径:
$path = config_path('dev/cms.php');
database_path()
这database_path
函数返回应用程序数据库目录的完全限定路径:
$path = database_path();
media_path($path = '')
这media_path
函数返回应用程序媒体目录的完全限定路径:
$path = media_path();
您也可以使用media_path
函数生成相对于媒体目录的给定文件的完全限定路径:
$path = media_path('images/myimage.png');
plugins_path($path = '')
这plugins_path
函数返回应用程序插件目录的完全限定路径:
$path = plugins_path();
您也可以使用plugins_path
函数生成相对于插件目录的给定文件的完全限定路径:
$path = plugins_path('author/plugin/routes.php');
public_path()
这public_path
函数返回到的完全限定路径public
目录:
$path = public_path();
storage_path($path = '')
这storage_path
函数返回到的完全限定路径storage
目录:
$path = storage_path();
您也可以使用storage_path
函数生成相对于存储目录的给定文件的完全限定路径:
$path = storage_path('app/file.txt');
temp_path($path = '')
这temp_path
函数返回临时文件的可写目录的完全限定路径:
$path = temp_path();
您也可以使用temp_path
函数生成相对于临时目录的给定文件的完全限定路径:
$path = temp_path('app/file.txt');
themes_path($path = '')
这themes_path
函数返回到的完全限定路径themes
目录:
$path = themes_path();
您也可以使用themes_path
函数生成相对于主题目录的给定文件的完全限定路径:
$path = themes_path('mytheme/file.txt');
uploads_path($path = '')
这uploads_path
函数返回应用程序上传目录的完全限定路径:
$path = uploads_path();
您也可以使用uploads_path
函数生成相对于上传目录的给定文件的完全限定路径:
$path = uploads_path('public/file.txt');
Strings
camel_case()
这camel_case
函数将给定的字符串转换为camelCase
:
$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>');
// <html>foo</html>
ends_with()
这ends_with
函数确定给定字符串是否以给定值结尾:
$value = ends_with('This is my name', 'name');
// true
snake_case()
这snake_case
函数将给定的字符串转换为snake_case
:
$snake = snake_case('fooBar');
// foo_bar
str_limit()
这str_limit
函数限制字符串中的字符数。该函数接受一个字符串作为其第一个参数,并接受最大结果字符数作为其第二个参数:
$value = str_limit('The CMS platform that gets back to basics.', 6);
// The CMS...
starts_with()
这starts_with
函数确定给定字符串是否以给定值开头:
$value = starts_with('The cow goes moo', 'The');
// true
str_contains()
这str_contains
函数确定给定字符串是否包含给定值:
$value = str_contains('The bird goes tweet', 'bird');
// 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
str_random()
这str_random
函数生成指定长度的随机字符串:
$string = str_random(40);
str_singular()
这str_singular
函数将字符串转换为其单数形式。此功能目前仅支持英文:
$singular = str_singular('cars');
// car
str_slug()
这str_slug
函数从给定的字符串生成一个 URL 友好的“slug”:
$title = str_slug("Winter CMS", "-");
// winter-cms
studly_case()
这studly_case
函数将给定的字符串转换为StudlyCase
:
$value = studly_case('foo_bar');
// FooBar
trans()
这trans
函数使用您的语言翻译给定的语言行本地化文件:
echo trans('validation.required'):
trans_choice()
这trans_choice
函数用词形变化翻译给定的语言行:
$value = trans_choice('foo.bar', $count);
SVG
Winter 包括一个简单的 SVG 实用程序,允许您从给定路径中提取经过清理的 SVG 标记。这可以是 对于清理或直接在主题中使用 SVG 标记很有用。
Svg::extract()
这extract
方法允许您在给定路径中提取经过清理的 SVG 标记。消毒防止使用
JavaScript、远程源和 CSS 导入,阻止 SVG 代码中的常见攻击媒介。
$svg = Svg::extract('/path/to/image.svg');
默认情况下,输出的 SVG 标记被缩小。第二个参数允许您通过将其设置为来禁用它false
.
$unminifiedSvg = Svg::extract('/path/to/image.svg', false);
Miscellaneous
asset()
使用请求的当前方案(HTTP 或 HTTPS)为资产生成 URL:
$url = asset('img/photo.jpg');
您可以通过设置资产 URL 主机来配置ASSET_URL
你的变量.env
文件(或asset_url
在你的config/app.php
文件)。如果您将资产托管在 Amazon S3 或其他 CDN 等外部服务上,这将很有用:
// ASSET_URL=http://example.com/assets
$url = asset('img/photo.jpg'); // http://example.com/assets/img/photo.jpg
config()
这config
函数获取配置变量的值。可以使用“点”语法访问配置值,其中包括文件名和您希望访问的选项。如果配置选项不存在,可以指定默认值并返回:
$value = config('app.timezone');
$value = config('app.timezone', $default);
这config
helper 也可用于通过传递键/值对数组在运行时设置配置变量:
config(['app.debug' => true]);
dd()
这dd
函数转储给定变量并结束脚本的执行:
dd($value);
env()
这env
函数获取环境变量的值或返回默认值:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
get()
这get
函数从请求中获取输入项,仅限于 GET 变量:
$value = get('key', $default = null)
input()
这input
函数从请求中获取输入项:
$value = input('key', $default = null)
post()
这post
函数从请求中获取一个输入项,仅限于 POST 变量:
$value = post('key', $default = null)
redirect()
这redirect
函数返回重定向器的一个实例来做重定向响应:
return redirect('/home');
request()
这request
函数返回当前请求实例:
$referer = request()->header('referer');
response()
这response
函数创建一个response 实例或获取响应工厂的实例:
return response('Hello World', 200, $headers);
return response()->json(['foo' => 'bar'], 200, $headers);
route()
这route
函数为给定的生成一个 URL命名路线:
$url = route('routeName');
如果路由接受参数,您可以将它们作为第二个参数传递给该方法:
$url = route('routeName', ['id' => 1]);
secure_asset()
使用 HTTPS 为资产生成 URL:
echo secure_asset('foo/bar.zip', $title, $attributes = []);
trace_log()
这trace_log
函数将跟踪消息写入日志文件。
trace_log('This code has passed...');
该函数支持传递异常、数组和对象:
trace_log($exception);
trace_log($array);
trace_log($object);
您还可以传递多个参数来跟踪多条消息:
trace_log($value1, $value2, $exception, '...');
trace_sql()
这trace_sql
函数启用数据库日志记录并开始监视所有 SQL 输出。
trace_sql();
Db::table('users')->count();
// select count(*) as aggregate from users
url()
这url
函数生成给定路径的完全限定 URL:
echo url('user/profile');
echo url('user/profile', [1]);