php 辅助函数,php爱好者_5个Laravel辅助函数让你的php开发更轻松

在Laravel框架中有许多的辅助函数可以帮助开发者更加有效率地进行开发。在这篇文章中,我会列出我个人比较喜欢的几个辅助函数。

01

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()’辅助方法能够让你轻松的再数组或者对象中使用相同的语法来查找数据。这样你就不必检查你之前使用的变量是什么类型了。

02

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。(https://github.com/doctrine/inflector/blob/master/lib/Doctrine/Common/Inflector/Inflector.php)

03

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的话,那么将会返回一个相对地址而不是一个绝对地址。

”’php

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

04

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(https://laravel.com/docs/5.6/authorization#gates)。 这样你就可以省去很多的abort检查了。

05

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

那么,你们最喜欢的辅助函数是哪些呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值