5 个让你的开发更加轻松的辅助函数
本文为翻译文章,原文章地址: 5 Laravel Helpers to Make Your Life Easier
在Laravel框架中有许多的辅助函数来帮助开发者更加有效率的进行开发。在这篇文章中,我会列出我个人比较喜欢的5个辅助函数
data_get()
data_get()
辅助方法能够让你使用[.]符号来获取数组或者对象中的值。’array_get()’方法也是同样的道理。如果数组或者对象的key不存在的话,这个方法第三个可选参数可以设置一个默认值。
$array = ['albums' => ['rock' => ['count' => 75 ]]];
$count = data_get($array, 'albums.rock.count'); // 75
$avgCost = data_get($array, 'albums.rock.avg_cost', 0); // 0
$object->albums->rock->count = 75;
$count = data_get($object, 'albums.rock.count'); // 75
$avgCost = data_get($object, 'albums.rock.avg_cost', 0); // 0
如果在点符号连接中使用通配符’*’将会返回一个数组。
$array = ['albums' => ['rock' => ['count' => 75], 'punk' => ['count' => 12]]];
$counts = data_get($array, 'albums.*.count'); // [75, 12]
‘data_get()’辅助方法能够让你轻松的再数组或者对象中使用相同的语法来查找数据。这样你就不必检查你之前使用的变量是什么类型了。
str_plural()
‘str_plural()’是将字符串变成对应的复数形式,目前只对英文的单词有效,第二个可选的参数能够让开发者来自己决定返回单数还是复数形式。
str_plural('dog'); // dogs
str_plural('cat'); // cats
str_plural('dog', 2); // dogs
str_plural('cat', 1); // cat
str_plural('child'); // children
str_plural('person'); // people
str_plural('fish'); // fish
str_plural('deer', 2); // deer
这个辅助方法最主要的用处就是能够移除类似 {{ $count == 1 ? ‘dog’ : ‘dogs’ }} 这样的代码。与之相反的还有一个’str_singular()’的辅助方法。 如果你感兴趣这个方法的工作原理,那么可以看看 Doctrine’s Inflector Class
route()
‘route()’方法能够生成已经命名的路由,可选的第二个参数将会传递给路由的参数。
Route::get('burgers', 'BurgersController@index')->name('burgers');
route('burgers'); // http://example.com/burgers
route('burgers', ['order_by' => 'price']); // http://example.com/burgers?order_by=price
Route::get('burgers/{id}', 'BurgersController@show')->name('burgers.show');
route('burgers.show', 1); // http://example.com/burgers/1
route('burgers.show', ['id' => 1]); // http://example.com/burgers/1
Route::get('employees/{id}/{name}', 'EmployeesController@show')->name('employees.show');
route('employees.show', [5, 'chris']); // http://example.com/employees/5/chris
route('employees.show', ['id' => 5, 'name' => 'chris']); // http://example.com/employees/5/chris
route('employees.show', ['id' => 5, 'name' => 'chris', 'hide' => 'email']); // http://example.com/employees/5/chris?hide=email
如果将第三个可选参数设为false的话,那么将会返回一个相对地址而不是一个绝对地址
route('burgers.show', 1, false); // /burgers/1
设置了子域名的也是同样的道理, 并且你也可以将Eloquent模型传参给route()方法
Route::domain('{location}.example.com')->group(function () {
Route::get('employees/{id}/{name}', 'EmployeesController@show')->name('employees.show');
});
route('employees.show', ['location' => 'raleigh', 'id' => 5, 'name' => 'chris']);
route('burgers.show', Burger::find(1)); // http://example.com/burgers/1
abort_if()
这个辅助方法将会抛出一个异常如果符合满足的要求。第三个可选参数为抛出异常的消息,第四个为header数组。
abort_if(! Auth::user()->isAdmin(), 403);
abort_if(! Auth::user()->isAdmin(), 403, 'Sorry, you are not an admin');
abort_if(Auth::user()->isCustomer(), 403);
这个方法最主要的用处就是精简类似下面的代码,通过使用 abort_if() 能够只用一行代码实现同样的功能。
// In "admin" specific controller
public function index()
{
if (! Auth::user()->isAdmin()) {
abort(403, 'Sorry, you are not an admin');
}
}
// better!
public function index()
{
abort_if(! Auth::user()->isAdmin(), 403);
}
注意 如果你是通过这个来控制权限的话,你应该了解一下 authorization gates。 这样你就可以省去很多的abort检查了。
optional()
这个方法允许你来获取对象的属性或者调用方法。如果该对象为null,那么属性或者方法也会返回null而不是引起一个错误
// User 1 exists, with account
$user1 = User::find(1);
$accountId = $user1->account->id; // 123
// User 2 exists, without account
$user2 = User::find(2);
$accountId = $user2->account->id; // PHP Error: Trying to get property of non-object
// Fix without optional()
$accountId = $user2->account ? $user2->account->id : null; // null
$accountId = $user2->account->id ?? null; // null
// Fix with optional()
$accountId = optional($user2->account)->id; // null
那么,你们最喜欢的辅助函数是哪些呢?
本作品采用《CC 协议》,转载必须注明作者和本文链接
optional还是很有用,,route从刚开始学laravel就一直用
学习了
@largezhou optional 有时候会想不起来用?
optional 省去了我很多长短的判断对象是否为空
赞一个?
感谢分享
optional 是个机智的方法
如果是在视图里面,可以这样写:{{ $user2->account->id or '' }}
看完以后一下子解决了好多问题,哈哈
abort_if 最近刚好用到了
optional 确实挺不错,这样省去了isset empty 个人感觉php判断是否为空的成本有点高啊
optional()怎么用呢,,我在laravel直接写,没有这个函数呢
@Complicated optional应该是5.5以后才加入的方法,如果低于这个版本的话是没有的。
@Epona 是这样的,刚才忘了,,应该有版本的要求,不过试了一下,如果是empty,,这就话就自动跳过了,不会提示什么的
@一根毛毛闯天下 很关键,兄dei
optional写法可能是借鉴swift得来的,swift里面的❓有更多高级用法
@powercen 是的,laravel里的optional有些地方没有 swift 里的灵活
刚看到optional就用到了,很实用的函数啊 哈哈哈
原来还有optional这个用法,Laravel 5.5及以上,备忘下。
PHP 8 新增
?->
使用$user2->account?->id