Laravel?5.1.11包含了對(duì)授權(quán)和策略的支持,將這些新特性整合到已經(jīng)存在的Laravel 5.1中很簡單。
注意:這些更新是可選的,忽略它們并不影響你的應(yīng)用。
首先,在應(yīng)用中創(chuàng)建一個(gè)空的app/Policies
目錄。
在app/Providers
目錄下創(chuàng)建AuthServiceProvider
,你可以從GitHub上拷貝該默認(rèn)提供者的內(nèi)容,創(chuàng)建完成后,確保在配置文件app.php
的providers
數(shù)組中注冊該提供者。
此外,還要在配置文件app.php
的aliases
數(shù)組中注冊Gate
門面:
'Gate' => Illuminate\Support\Facades\Gate::class,
然后,在App\User
?模型中使用?Illuminate\Foundation\Auth\Access\Authorizable
?trait并實(shí)現(xiàn)Illuminate\Contracts\Auth\Access\Authorizable
契約:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
接下來,更新基類控制器App\Http\Controllers\Controller
以便可以使用Illuminate\Foundation\Auth\Access\AuthorizesRequests
trait:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
abstract class Controller extends BaseController{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
預(yù)計(jì)更新時(shí)間:小于1小時(shí)
更新bootstrap/autoload.php
中的變量$compilePath:
$compiledPath = __DIR__.'/cache/compiled.php';
在bootstrap
目錄中,創(chuàng)建cache
目錄,在該目錄中創(chuàng)建一個(gè).gitignore
文件,編輯文件內(nèi)容如下:
*!.gitignore
該目錄應(yīng)該是可寫的,用來存儲(chǔ)臨時(shí)優(yōu)化文件如compiled.php
,routes.php
,config.php
以及service.json
在配置文件config/app.php
中,添加Illuminate\Broadcasting\BroadcastServiceProvider
到providers
數(shù)組。
如果你在使用Laravel自帶的AuthenticatesAndRegistersUsers
的AuthController
,則需要對(duì)新用戶的驗(yàn)證和創(chuàng)建做一些代碼改動(dòng):
首先,你不再需要傳遞Guard
和Register
實(shí)例到構(gòu)造函數(shù),你可以從控制器的構(gòu)造器中完全移除這些以依賴。
然后,Laravel 5.0中使用的App\Services\Registrar
不再被需要,你可以直接簡單拷貝粘貼其中的validator
方法和create
方法到AuthController
中,這兩個(gè)方法中的代碼不需要做任何改動(dòng)。不要忘記確認(rèn)Validator
和User
在AuthController
中是否已經(jīng)被導(dǎo)入。
PasswordController
不再需要在構(gòu)造函數(shù)中聲明任何依賴,可以移除5.0中要求的兩個(gè)依賴。
如果你重寫了Controller
類中的formatValidationErrors
方法,需要將類型提示由Illuminate\Validation\Validator
改為Illuminate\Contracts\Validation\Validator
。
create方法
Eloquent的create
方法現(xiàn)在可以不傳入任何參數(shù)進(jìn)行調(diào)用,如果你在模型中要重寫create
方法,將$attributes
參數(shù)的默認(rèn)值改為數(shù)組:
public static function create(array $attributes = []){
// Your custom implementation
}
find方法
如果你要在自己的模型中重寫find
方法并在其中調(diào)用parent::find()
,應(yīng)該改由調(diào)用Eloquent查詢構(gòu)建器的find
方法:
public static function find($id, $columns = ['*']){
$model = static::query()->find($id, $columns);
// ...
return $model;
}
lists方法
lists
方法現(xiàn)在返回一個(gè)Collection
實(shí)例而不是包含Eloquent查詢結(jié)果的數(shù)組,如果你想將Collection轉(zhuǎn)化為數(shù)組,使用all
方法:
User::lists('id')->all();
注意:Query Builder的lists返回的仍然是數(shù)組。
日期格式化
以前,模型中的Eloquent日期字段存儲(chǔ)格式可以通過重寫getDateFormat
方法來修改,現(xiàn)在依然可以這么做;但是為了更加方便可以在模型中簡單通過指定$dateFormat
屬性來替代重寫方法。
在序列化模型到數(shù)組或JSON時(shí)日期格式也被應(yīng)用到,當(dāng)從Laravel 5.0遷移到5.1時(shí),這將會(huì)改變JSON序列化的日期字段的格式。想要在序列化模型中設(shè)置指定的日期格式,你可以在模型中重寫serializeDate(DateTime?$date)
方法,這樣就可以在不改變字段存儲(chǔ)格式的情況下對(duì)格式化序列化的Eloquent日期字段有著更加細(xì)粒度的控制。
sortBy方法
sortBy方法現(xiàn)在返回一個(gè)新的collection實(shí)例而不是改變已有的collection:
$collection = $collection->sortBy('name');
groupBy方法
groupBy
方法現(xiàn)在為每個(gè)父級(jí)Collection中的item返回Collection實(shí)例,如果你想要將這些items轉(zhuǎn)化為數(shù)組,可以通過map
方法實(shí)現(xiàn):
$collection->groupBy('type')->map(function($item){
return $item->all();
});
lists方法
lists方法現(xiàn)在返回一個(gè)Collection實(shí)例而不是數(shù)組,如果你想要將Collection轉(zhuǎn)化數(shù)組,使用all
方法:
$collection->lists('id')->all();
app/Commands
目錄現(xiàn)在被重命名為app/Jobs
,但是并不需要將你的命令移動(dòng)到新位置,你可以繼續(xù)使用make:command
和handler:command
?Artisan 命令生成自己的類。
同樣的,app/Handlers
目錄被合并到app/Listeners
目錄下,你也不必將已經(jīng)存在的命令和事件處理器進(jìn)行移動(dòng)和重命名,你可以繼續(xù)使用handler:event
命令生成事件處理器。
通過提供對(duì)Laravel 5.0目錄結(jié)構(gòu)的向后兼容,你可以無縫升級(jí)應(yīng)用到Laravel 5.1然后慢慢升級(jí)你的事件和命令到新的位置——在一個(gè)對(duì)你或你的團(tuán)隊(duì)合適的時(shí)間。
createMatcher
,createOpenMatcher
和createPlainMatcher
方法已經(jīng)從Blade編譯器中移除,可以使用新的directive
方法來為5.1版的Blade創(chuàng)建自定義的指令。查閱擴(kuò)展Blade文檔了解詳情。
在tests/TestCase.php文件中新增protected屬性$baseUrl
:
protected $baseUrl = 'http://localhost';
用于為vendor包發(fā)布語言文件的默認(rèn)目錄做了移動(dòng),所有vendor包語言文件從resources/lang/packages/{locale}/{namespace}
移動(dòng)到了resources/lang/vendor/{namespace}/{locale}
目錄。例如,Acme/Anvil
包的acme/anvil::foo
英語語言文件將會(huì)從resources/lang/packages/en/acme/anvil/foo.php
移動(dòng)到resources/lang/vendor/acme/anvil/en/foo.php
。
如果你正在使用AWS SQS隊(duì)列驅(qū)動(dòng)或者AWS SES電子郵件驅(qū)動(dòng),需要升級(jí)AWS PHP SDK到3.0版本。
如果你正在使用Amazon S3文件系統(tǒng)驅(qū)動(dòng),需要通過Composer升級(jí)相應(yīng)的文件系統(tǒng)包:
league/flysystem-aws-s3-v3?~1.0
以下Laravel特性已經(jīng)被廢棄并且會(huì)在2015年12月份的Laravel 5.2中被完全移除:
Illuminate\Contracts\Routing\Middleware
,中間件中不再需要任何contract,Illuminate\Contracts\Routing\TerminableMiddleware
被廢棄,在中間件中定義一個(gè)terminate方法替代實(shí)現(xiàn)該接口。Illuminate\Contracts\Queue\ShouldBeQueued
?被廢棄,使用?Illuminate\Contracts\Queue\ShouldQueue
Illuminate\Foundation\Bus\DispatchesCommands
?trait 被廢棄并被重命名為Illuminate\Foundation\Bus\DispatchesJobs
.Illuminate\Container\BindingResolutionException
?被移動(dòng)到Illuminate\Contracts\Container\BindingResolutionException
.bindShared
?方法被廢棄,使用singleton
?方法。pluck
?方法被廢棄并重命名為value
.fetch
?方法被廢棄,使用?pluck
?方法.array_fetch
?幫助函數(shù)被廢棄, 使用array_pluck
更多建議: