建議如下情況進(jìn)行升級
以下內(nèi)容部分來自官方文檔。由于我建議全部添加命名空間,內(nèi)容和文檔有出入,并有些內(nèi)容文檔未提及
新建一個L5項目,新建方法參考這里,然后拷貝L4的文件到新建的項目下面。
拷貝的文件包括:controller, routes, models, Artisan commands, assets, 還有一些你自己添加的類或者資源。
拷貝你添加的所有的composer依賴和包到L5的 composer.json
中,也包括你引用的其他的代碼和SDK。
不過需要注意一點就是,你依次去那些針對Laravel開發(fā)的包需要到項目主頁看看作者是否支持L5或者說準(zhǔn)備支持L5,據(jù)我所知,目前主流的包基本已支持,因為改動不是特別大。選好支持L5的版本之后, composer
update
就好了。
L4的命名空間是全局的。雖然官方說能不加命名空間就能遷移,但是還是手動給加上吧!不然以后更麻煩了。提醒一下,有這個方法可以修改命名空間的前綴: php
artisan app:name Yourproj
。
如果你的程序中使用了變量作為動態(tài)類名,一定要注意在變量中添加完整的命名空間:
# L4中可能存在的寫法 $myClassName = 'Dog'; $obj = new $myClassName(); // 在L5中將要報錯 # L5中要修改為 $myClassName = 'app\\Models\\Dog'; $obj = new $myClassName();
項目根目錄命令行 cp
.env.example .env
,拷貝你自定義的配置到這里,配置文件不再像之前那樣有很多文件夾供你根據(jù)環(huán)境選擇了,L5下只有這一個,意思就是每個不同的環(huán)境都需要自己來稍微定制一些。不過每個項目下面可能都是不同的。寫好配置文件后記得保存?zhèn)€模板到 .env.example
供其他隊友使用。
在 config/ 下面開始使用 env('DB_HOST',
'localhost')
的方式來調(diào)用你的配置到對應(yīng)的數(shù)組鍵下面。
拷貝原來的 routes.php
到 app/Http/routes.php
拷貝你的 contollers
到 app/Http/Controllers
下。添加正確的命名空間到每個類上App\Http\Controllers
。記得讓你的 BaseController
繼承那個抽象類 Controller
。然后挨個查看文件,根據(jù)PHPstorm提示進(jìn)行糾錯,主要包括引用類和命名空間的錯誤。
新建文件夾到 app/Models
,把原來的 models
全部拷貝過來。首先,添加命名空間 App\Models
。接著是關(guān)聯(lián)到其他model的一些方法,比如
belongTo, hasMany等,第一個參數(shù)需要填寫完整的命名空間,例如
class User extends Eloquent {
public function phone()
{
// return $this->hasOne('Phone'); 原來這樣寫的
return $this->hasOne('App\Models\Phone'); // L5需要添加完整命名空間
}
}
L5中的中間件 Middleware
是個重頭戲,路由 routes.php
中的 ['before'
=> 'auth']
需要替換為['middleware'
=> 'auth']
。
同時還要改一下過濾器Filters:
// app/filters.php
Router::filter('shall-not-pass', function() {
return Redirect::to('shadow');
});
改成這樣子
// app/Providers/RouteServiceProvider@boot()
$router->filter('shall-not-pass', function() {
return \Redirect::to('shadow');
});
Builder 不再支持 remember
這個方法了,請使用 Cache::remember
對程序改造
。如果使用了 redis
,還需要 composer
require 'predis/predis'
。
按照下面的操作對 User
model
進(jìn)行升級。
刪除下面的內(nèi)容
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
然后添加以下代碼:
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
刪除 UserInterface
和 RemindableInterface
這兩個接口,然后添加 AuthenticatableContract
和CanResetPasswordContract
這兩個接口。
添加以下兩個 traits
到類里面
use Authenticatable, CanResetPassword;
如果你用到Illuminate\Auth\Reminders\RemindableTrait
和Illuminate\Auth\UserTrait
,那么就把他們刪掉。
直接拷貝你的命令行程序的文件到 app/Console/Cammands
目錄,并添加對應(yīng)命名空間。
接著拷貝 start/artisan.php
內(nèi)容到 app/Console/Kernel.php
文件的 command
數(shù)組中。例如
protected $commands = [
'Laracasts\Console\Commands\ClearHistoryCommand',
'Laracasts\Console\Commands\SignupsReportCommand',
'Laracasts\Console\Commands\WelcomeUserCommand',
];
刪除L5 database/migrations
中自帶的兩個數(shù)據(jù)遷移文件,然后把你自己原來的數(shù)據(jù)庫遷移文件從app/database/migrations
拷貝到 database/migrations
中來。 app/database/seeds
的文件拷貝到database/seeds
中。
這個操作不需要添加命名空間,因為在 composer.json
中已經(jīng)引入了該目錄。
如果在 start/global.php
中有ioc綁定的話,那就吧他們移動到 app/Providers/AppServiceProvider.php
的 register
方法中。同時還需要引入 App
facade
。
直接從 app/views
復(fù)制到 resources/views
中。
L4中的 {{
}}
對應(yīng)為L5的 {!!
!!}
,而L4中的 {{{
}}}
對應(yīng)L5的 {{
}}
。需要對應(yīng)修改一下。
復(fù)制 app/lang
到 resources/lang
把你的公共資源都直接拷貝過去吧!
復(fù)制 app/tests
到 tests
目錄。
如果你用了 Form
或者 HTML
幫助函數(shù),那么就在 composer.json
中添加 "illuminate/html":
"~5.0"
。
然后在 config/app.php
中添加 'providers'
:
'Illuminate\Html\HtmlServiceProvider',
接著在 'aliases'
中添加:
'Form' => 'Illuminate\Html\FormFacade',
'Html' => 'Illuminate\Html\HtmlFacade',
替換 $paginator->links()
為 $paginator->render()
。如果你這里使用了分頁模板的話,L4是在links中傳入分頁模板的路徑字符串,而L5中render的參數(shù)為Illuminate\Contracts\Pagination\Presenter對象,需要根據(jù)需要建立一個繼承該接口的類。
L5對應(yīng)的 Beanstalk
包為: "pda/pheanstalk":
"~3.0"
,不再是 "pda/pheanstalk":
"~2.1"
相信你按照上面的步驟執(zhí)行后,你的程序依然報錯。因為自己的項目都可能有一些比較 個性 的地方,所以需要多加細(xì)心和耐心來完成糾錯。
如果你使用了xdebug的斷點調(diào)試,可能會讓你事半功倍。
遇到問題了歡迎來探討!
最后祝你 level up !^^
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com