Laravel进阶系列笔记--(十七)Laravel 辅助函数
作者:温新
时间:2021-07-12
hi,我是温新,一名PHPer。
本篇文章记录Laravel自带的PHP辅助函数,如之前用户的接收用户请求数据的request()
辅助函数,都在这里。
数组和对象
使用Illuminate\Support\Arr
门面。
accessible
函数:accessible()
检测给定的值是否可以通过数组方式访问。
返回值 :可以为 true,否则为 false
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 0, 0)">Route</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">get</span>(<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'test'</span>, <span style="box-sizing: border-box;color: rgb(119, 0, 136)">function</span>(){</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(170, 85, 0)">// true</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">accessible</span>([<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'lisi'</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'age'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(17, 102, 68)">12</span>]);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(170, 85, 0)">// false</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test1</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">accessible</span>(<span style="box-sizing: border-box;color: rgb(17, 102, 68)">19</span>);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">dd</span>(<span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test</span>);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">});</span>
add
函数:add(arr,key,value)
添加给定的键值对到数组。
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">add</span>([<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'lisi'</span>],<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'age'</span>,<span style="box-sizing: border-box;color: rgb(17, 102, 68)">19</span>);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 0, 0)">dd</span>(<span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test</span>);</span>
collapse
函数:collapse()
合并多个数组。
$test = \Illuminate\Support\Arr::collapse([['name'=>'lisi'],['age'=>19]]);
crossJoin
函数:crossJoin
返回笛卡尔乘积数组
$test = \Illuminate\Support\Arr::crossJoin(['a','b'],['c','d']);
divide
函数:divide()返回一个包含数组key和值的数组
$test = \Illuminate\Support\Arr::divide(['name'=>'lisi','age'=>19]);
dot
函数:dot()
使用 .
将多维数组转为一维数组
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$test</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">dot</span>([<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'class'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span>[<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'one'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span>[<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'lisi'</span>],<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'two'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span>[<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'wangwu'</span>]]]);</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(170, 85, 0)">// 返回值</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(119, 0, 136)">array</span>:<span style="box-sizing: border-box;color: rgb(17, 102, 68)">2</span> [<span style="box-sizing: border-box;color: rgb(0, 0, 0)">▼</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">"class.one.name"</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">"lisi"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">"class.two.name"</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span> <span style="box-sizing: border-box;color: rgb(170, 17, 17)">"wangwu"</span></span><br></br><span style="box-sizing: border-box;padding-right: 0.1px">]</span>
exists
函数:exists
检查给定的键是否在数组中
返回值:在数组中返回 true,不在返回 false
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$arr</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> [<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'lisi'</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'age'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(17, 102, 68)">19</span>];</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$exists</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">exists</span>(<span style="box-sizing: border-box;color: rgb(0, 85, 170)">$arr</span>, <span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span>);;</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 0, 0)">dd</span>(<span style="box-sizing: border-box;color: rgb(0, 85, 170)">$exists</span>);</span>
except
函数:except()
从数组中移除给定的键值对
<span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$arr</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> [<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(170, 17, 17)">'lisi'</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'age'</span><span style="box-sizing: border-box;color: rgb(152, 26, 26)">=></span><span style="box-sizing: border-box;color: rgb(17, 102, 68)">19</span>];</span><br></br><span style="box-sizing: border-box;padding-right: 0.1px"><span style="box-sizing: border-box;color: rgb(0, 85, 170)">$res</span> <span style="box-sizing: border-box;color: rgb(152, 26, 26)">=</span> <span style="box-sizing: border-box;color: rgb(0, 0, 0)">\Illuminate\Support\Arr</span>::<span style="box-sizing: border-box;color: rgb(0, 0, 0)">except</span>(<span style="box-sizing: border-box;color: rgb(0, 85, 170)">$arr</span>, [<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'name'</span>,<span style="box-sizing: border-box;color: rgb(170, 17, 17)">'age'</span>]);</span>
first
函数:first
返回符合条件的第一个值
$arr = [1,2,3,4,5,3];
$first = \Illuminate\Support\Arr::first($arr, function($value, $key){
return $value >= 3;
});
// 3
dd($first);
flatten
函数:flatten
将多维数组转化为一维数组
$arr = ['name'=>'lisi','age'=>19,'alist'=>[
'xiaoliu','wangmazi'
]];
$res = \Illuminate\Support\Arr::flatten($arr);
get
函数:get()
使用 .
从嵌套数组中获取值
$arr = ['name'=>'lisi','desk'=>['sex'=>1]];
$res = \Illuminate\Support\Arr::get($arr,'desk.sex');
has
函数:has()
使用.
检测给定的数据是否在数组中
返回值:在 返回 true,不在返回 false
$arr = ['name'=>'lisi','desk'=>['sex'=>1]];
$res = \Illuminate\Support\Arr::has($arr,'desk.sex');
last
函数:last()
返回符合条件的最后一个元素
$arr = [1,2,3,4,5,3,9];
$first = \Illuminate\Support\Arr::last($arr, function($value, $key){
return $value >= 3;
});
// 9
dd($first);
prepend
函数:prepend()
从数组开头插入数据
$arr = [4,5,6];
$res = \Illuminate\Support\Arr::prepend($arr,1);
dd($res);
query
函数:query()
把数组转化为查询字符串
$arr = ['keyworld'=>'lisi','name'=>'wangwu'];
$res = \Illuminate\Support\Arr::query($arr);
dd($res); // "keyworld=lisi&name=wangwu"
路径函数
app_path
函数:app_path()
返回app目录的绝对路径
Route::get('test', function(){
return app_path();
});
base_path()
函数:base_path()
返回项目根路径的绝对路径
Route::get('test', function(){
return base_path();
});
config_path()
函数:config_path()
返回应用配置目录 config
的绝对路径
public_path()
函数:public_path()
返回public
目录的绝对路径
resource_path()
函数:resource_path()
返回resources
目录的绝对路径
storage_path()
函数:storage_path()
返回storage
目录的绝对路径
字符串函数
字符串函数使用Illuminate\Support\Str
门面
after()
函数:after()
返回给定字符串之后的所有内容
// 返回 ziruchu
$str = \Illuminate\Support\Str::after('my name is ziruchu','is');
afterLast()
函数:afterLast()
返回字符串中给定值最后一次出现位置的所有字符串
// 返回Controller
$str = \Illuminate\Support\Str::afterLast('App\Http\Controllers\Controller', '\\');
before()
函数:before()
函数返回字符串中给定值之前的所有字符串
// 返回 my name
$str = \Illuminate\Support\Str::before('my name is ziruchu','is');
beforeLast()
函数:beforeLast()
返回给定字符串中最后一次出现位置之前的所有字符串
// 返回my
$str = \Illuminate\Support\Str::beforeLast('my name is ziruchu','name');
between()
函数:between()
返回两个值之间的部分字符串
// 返回 name is
$str = \Illuminate\Support\Str::between('my name is ziruchu','my', 'ziruchu');
camel()
函数:camel()
将给定字符串转为驼峰格式
// 返回 ziRuChu
$str = \Illuminate\Support\Str::camel('zi_ru_chu');
contains()
函数:contains()
判断给定字符串是否包含给定值(区分大小写)
$str = \Illuminate\Support\Str::contains('this is my ziruchu','ziruchu');
containsAll()
函数:containsAll()
判断给定字符串是否包含所有数组值
$str = \Illuminate\Support\Str::contains('this is my ziruchu',['is','my']);
length()
函数:length()
返回给定字符串的长度
$str = \Illuminate\Support\Str::length('ziruchu');
lower()
函数:lower()
将给定字符串转化为小写
$str = \Illuminate\Support\Str::lower('ZIRUCHU');
random()
函数:random()
生成指定长度的随机字符串
$str = \Illuminate\Support\Str::random(30);
substr()
函数:substr()
截取字符串
// is my
$str = \Illuminate\Support\Str::substr('this is my name ziruchu', 4,7);
URL函数
action()
函数:action()
为给定的控制器动作生成URL
// 返回 域名/user/add
$url = action('UserController@add');
asset()
函数:asset()
为前端资源生成URL
$url = asset('img/photo.jpg');
route()
函数:route()
为命名路由生成URL
$url = route('user.add');
secure_asset()
函数:secure_asset()
使用HTTPS为前端资源生成URL
$url = secure_asset('img/photo.jpg');
url()
函数:url()
为给定路径生成完成的URL
$url = url('detail',['id'=>1]);
其它函数
abort()
函数:abort()
抛出一个异常
abort(404);
app()
函数:app()
返回服务容器实例
$container = app();
back()
函数:back()
生成HTTP重定向到用户前一个访问页面
return back();
bcrypt()
函数:bcrypt()
进行哈希加密
$password = bcrypt('123');
config()
函数:config()
获取配置值
$configValue = config('app.timezone');
csrf_field()
函数:csrf_field()
生成一个包含CSRF验证的令牌值
{{ csrf_field() }}
csrf_token()
函数:csrf_token()
获取当前CSRF令牌值
$token = csrf_token();
dd()
函数:dd()
打印变量值并终止程序
dd(1);
dump()
函数:dump()
打印变量值,不终止程序
decrypt()
函数:decrypt()
使用Laravel加密器对给定值进行解密
dispatch()
函数:dispatch()
推荐一个新的任务到Laravel任务队列
dispatch(new App\Jobs\TestJob);
encrypt()
函数:encrypt()
使用Laravel加密器对给定的值加密
now()
函数:now()
为当前时间创建一个新的 Illuminate\Support\Carbon
实例
return now();
method_field()
method_field
函数生成包含 HTTP 请求方法的 HTML hidden
表单字段
<form method="POST">
{{ method_field('DELETE') }}
</form>
redirect()
函数:redirect()
重定向
return redirect('/');
request()
函数:request()
获取当前请求数据
$data = request()->input();
关于辅助函数,还有很多方法,请参考文档。
我是温新
每天进步一点点,就一点点