助手

October包含各种PHP“帮助”函数。 其中许多函数在October内部使用,但是,如果您发现它们很有用,可以在自己的插件和应用程序中自由使用它们。

Arrays 数组

array_add, array_divide, array_dot, 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 路径

Path Symbols, app_path, base_path, config_path, database_path, plugins_path, public_path, storage_path, temp_path, themes_path, uploads_path,

Strings 字符串

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,

Miscellaneous 其他

asset, config, dd, env, get, input, post, redirect, request, response, route, secure_asset, trace_log, trace_sql, url,

Arrays 数组

array_add()

如果数组中不存在给定的键,array_add函数会将给定的键/值对添加到数组中:

  1. $array = array_add(['name' => 'Desk'], 'price', 100);
  2. // ['name' => 'Desk', 'price' => 100]

array_divide()

array_divide函数返回两个数组,一个包含键,另一个包含原始数组的值:

  1. list($keys, $values) = array_divide(['name' => 'Desk']);
  2. // $keys: ['name']
  3. // $values: ['Desk']

array_dot()

array_dot函数将多维数组展平为单级数组,使用“点”符号表示深度:

  1. $array = array_dot(['foo' => ['bar' => 'baz']]);
  2. // ['foo.bar' => 'baz'];

array_except()

array_except方法从数组中删除给定的键/值对:

  1. $array = ['name' => 'Desk', 'price' => 100];
  2. $array = array_except($array, ['price']);
  3. // ['name' => 'Desk']

array_first()

array_first方法返回通过给定真值测试的数组的第一个元素:

  1. $array = [100, 200, 300];
  2. $value = array_first($array, function ($key, $value) {
  3. return $value >= 150;
  4. });
  5. // 200

默认值也可以作为第三个参数传递给方法。 如果没有值通过真值测试,则返回此值:

  1. $value = array_first($array, $callback, $default);

array_flatten()

array_flatten方法将多维数组展平为单个级别。

  1. $array = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']];
  2. $array = array_flatten($array);
  3. // ['Joe', 'PHP', 'Ruby'];

array_forget()

array_forget方法使用“点”表示法从深层嵌套的数组中删除给定的键/值对:

  1. $array = ['products' => ['desk' => ['price' => 100]]];
  2. array_forget($array, 'products.desk');
  3. // ['products' => []]

array_get()

array_get方法使用“点”表示法从深层嵌套数组中检索值:

  1. $array = ['products' => ['desk' => ['price' => 100]]];
  2. $value = array_get($array, 'products.desk');
  3. // ['price' => 100]

array_get函数也接受一个默认值,如果找不到特定的键,将返回该值:

  1. $value = array_get($array, 'names.john', 'default');

array_only()

array_only方法只返回给定数组中指定的键/值对:

  1. $array = ['name' => 'Desk', 'price' => 100, 'orders' => 10];
  2. $array = array_only($array, ['name', 'price']);
  3. // ['name' => 'Desk', 'price' => 100]

array_pluck()

array_pluck方法将从数组中获取给定键/值对的列表:

  1. $array = [
  2. ['developer' => ['name' => 'Brian']],
  3. ['developer' => ['name' => 'Stewie']]
  4. ];
  5. $array = array_pluck($array, 'developer.name');
  6. // ['Brian', 'Stewie'];

array_pull()

array_pull方法返回并从数组中删除键/值对:

  1. $array = ['name' => 'Desk', 'price' => 100];
  2. $name = array_pull($array, 'name');
  3. // $name: Desk
  4. // $array: ['price' => 100]

array_set()

array_set方法使用“点”表示法在深层嵌套数组中设置一个值:

  1. $array = ['products' => ['desk' => ['price' => 100]]];
  2. array_set($array, 'products.desk.price', 200);
  3. // ['products' => ['desk' => ['price' => 200]]]

array_sort()

array_sort方法按给定Closure的结果对数组进行排序:

  1. $array = [
  2. ['name' => 'Desk'],
  3. ['name' => 'Chair'],
  4. ];
  5. $array = array_values(array_sort($array, function ($value) {
  6. return $value['name'];
  7. }));
  8. /*
  9. [
  10. ['name' => 'Chair'],
  11. ['name' => 'Desk'],
  12. ]
  13. */

array_sort_recursive()

array_sort_recursive函数使用sort函数递归地对数组进行排序:

  1. $array = [
  2. [
  3. 'Brian',
  4. 'Shannon',
  5. 'Alec',
  6. ],
  7. [
  8. 'PHP',
  9. 'Ruby',
  10. 'JavaScript',
  11. ],
  12. ];
  13. $array = array_sort_recursive($array);
  14. /*
  15. [
  16. [
  17. 'Alec',
  18. 'Brian',
  19. 'Shannon',
  20. ],
  21. [
  22. 'JavaScript',
  23. 'PHP',
  24. 'Ruby',
  25. ]
  26. ];
  27. */

array_where()

array_where函数使用给定的Closure过滤数组:

  1. $array = [100, '200', 300, '400', 500];
  2. $array = array_where($array, function ($key, $value) {
  3. return is_string($value);
  4. });
  5. // [1 => 200, 3 => 400]

head()

head函数只返回给定数组中的第一个元素:

  1. $array = [100, 200, 300];
  2. $first = head($array);
  3. // 100

last()

last函数返回给定数组中的最后一个元素:

  1. $array = [100, 200, 300];
  2. $last = last($array);
  3. // 300

Paths 路径

路径符号

路径前缀符号可用于创建动态路径。 例如,以〜/开头的路径将创建相对于应用程序的路径:

  1. list: ~/plugins/acme/pay/models/invoiceitem/columns.yaml

支持这些符号来创建动态路径:

符号 描述
$ 相对于plugins目录
~ 相对于应用程序目录

app_path()

app_path函数返回app目录的全路径:

  1. $path = app_path();

您还可以使用app_path函数生成相对于应用程序目录的给定文件的全路径:

  1. $path = app_path('Http/Controllers/Controller.php');

base_path()

base_path函数返回项目根目录的全路径:

  1. $path = base_path();

您还可以使用base_path函数生成相对于应用程序目录的给定文件的全路径:

  1. $path = base_path('vendor/bin');

config_path()

config_path函数返回应用程序配置目录的全路径:

  1. $path = config_path();

database_path()

database_path函数返回应用程序数据库目录的全路径:

  1. $path = database_path();

plugins_path()

plugins_path函数返回应用程序插件目录的全路径:

  1. $path = plugins_path();

public_path()

public_path函数返回public目录的全路径:

  1. $path = public_path();

storage_path()

storage_path函数返回storage目录的全路径:

  1. $path = storage_path();

您还可以使用storage_path函数生成相对于存储目录的给定文件的全路径:

  1. $path = storage_path('app/file.txt');

temp_path()

temp_path函数返回临时文件的可写目录的全路径:

  1. $path = temp_path();

themes_path()

themes_path函数返回themes目录的全路径:

  1. $path = themes_path();

uploads_path()

uploads_path函数返回应用程序上传目录的全路径:

  1. $path = uploads_path();

Strings

camel_case()

camel_case函数将给定的字符串转换为驼峰格式

  1. $camel = camel_case('foo_bar');
  2. // fooBar

class_basename()

class_basename返回给定类的类名,删除了类的命名空间:

  1. $class = class_basename('Foo\Bar\Baz');
  2. // Baz

e()

e函数在给定字符串上运行htmlentities

  1. echo e('<html>foo</html>');
  2. // &lt;html&gt;foo&lt;/html&gt;

ends_with()

ends_with函数确定给定的字符串是否以给定值结束:

  1. $value = ends_with('This is my name', 'name');
  2. // true

snake_case()

snake_case函数将给定的驼峰格式字符串转换为下划线格式

  1. $snake = snake_case('fooBar');
  2. // foo_bar

str_limit()

str_limit函数限制字符串中的字符数。 该函数接受一个字符串作为其第一个参数,并将最终结果字符数作为其第二个参数:

  1. $value = str_limit('The CMS platform that gets back to basics.', 6);
  2. // The CMS...

starts_with()

starts_with函数确定给定的字符串是否以给定值开头:

  1. $value = starts_with('The cow goes moo', 'The');
  2. // true

str_contains()

str_contains函数确定给定的字符串是否包含给定值:

  1. $value = str_contains('The bird goes tweet', 'bird');
  2. // true

str_finish()

str_finish函数将给定值的单个实例添加到字符串:

  1. $string = str_finish('this/string', '/');
  2. // this/string/

str_is()

str_is函数确定给定的字符串是否与给定的模式匹配。 星号可用于表示通配符:

  1. $value = str_is('foo*', 'foobar');
  2. // true
  3. $value = str_is('baz*', 'foobar');
  4. // false

str_plural()

str_plural函数将字符串转换为复数形式。 此功能目前仅支持英语:

  1. $plural = str_plural('car');
  2. // cars
  3. $plural = str_plural('child');
  4. // children

str_random()

str_random函数生成一个指定长度的随机字符串:

  1. $string = str_random(40);

str_singular()

str_singular函数将字符串转换为单数形式。 此功能目前仅支持英语:

  1. $singular = str_singular('cars');
  2. // car

str_slug()

str_slug函数从给定的字符串生成一个URL友好的“slug”:

  1. $title = str_slug("October CMS", "-");
  2. // october-cms

studly_case()

studly_case函数将给定的字符串转换为StudlyCase

  1. $value = studly_case('foo_bar');
  2. // FooBar

trans()

trans函数使用本地化文件转换给定的语言行:

  1. echo trans('validation.required'):

trans_choice()

trans_choice函数使用拐义转换给定的语言行:

  1. $value = trans_choice('foo.bar', $count);

Miscellaneous 其他

asset()

使用当前的请求方案(HTTP或HTTPS)为资源生成URL:

  1. $url = asset('img/photo.jpg');

config()

config函数获取配置变量的值。 可以使用“点”语法访问配置值,该语法包括文件名和您要访问的选项。 可以指定默认值,如果配置选项不存在,则返回默认值:

  1. $value = config('app.timezone');
  2. $value = config('app.timezone', $default);

config帮助器也可以通过传递一组键/值对来在运行时设置配置变量:

  1. config(['app.debug' => true]);

dd()

dd函数转储给定变量并结束脚本的执行:

  1. dd($value);

env()

env函数获取环境变量的值或返回默认值:

  1. $env = env('APP_ENV');
  2. // Return a default value if the variable doesn't exist...
  3. $env = env('APP_ENV', 'production');

get()

get函数从请求中获取输入项,仅限于GET变量:

  1. $value = get('key', $default = null)

input()

input函数从请求中获取输入项:

  1. $value = input('key', $default = null)

post()

post函数从请求中获取输入项,仅限于POST变量:

  1. $value = post('key', $default = null)

redirect()

redirect函数返回重定向器的一个实例来做重定向响应::

  1. return redirect('/home');

request()

request函数返回当前的request instance

  1. $referer = request()->header('referer');

response()

response函数创建response实例或获取响应工厂的实例:

  1. return response('Hello World', 200, $headers);
  2. return response()->json(['foo' => 'bar'], 200, $headers);

route()

route函数为给定的命名路由生成一个URL:

  1. $url = route('routeName');

如果路由接受参数,您可以将它们作为方法的第二个参数传递:

  1. $url = route('routeName', ['id' => 1]);

secure_asset()

使用HTTPS为资源生成URL:

  1. echo secure_asset('foo/bar.zip', $title, $attributes = []);

trace_log()

trace_log函数将跟踪消息写入日志文件。

  1. trace_log('This code has passed...');

该函数支持传递异常,数组和对象:

  1. trace_log($exception);
  2. trace_log($array);
  3. trace_log($object);

您还可以传递多个参数来跟踪多条消息:

  1. trace_log($value1, $value2, $exception, '...');

trace_sql()

trace_sql函数启用数据库日志记录并开始监视所有SQL输出。

  1. trace_sql();
  2. Db::table('users')->count();
  3. // select count(*) as aggregate from users

url()

url函数生成给定路径的URL:

  1. echo url('user/profile');
  2. echo url('user/profile', [1]);