Laravel 4.2 升级 Laravel 5.0 攻略

来源:互联网 发布:网站编程 编辑:程序博客网 时间:2024/06/01 08:24

Laravel 4.2 升级 Laravel 5.0 攻略

转自

https://phphub.org/topics/474


以下所指L4为laravel 4.2,L5为laravel 5.0

建议如下情况进行升级

  1. 对L4比较了解,同时对L5有基本的认识
  2. 想对比L4和L5的差异,快速学习L5
  3. 程序的代码写的不乱,按照Laravel的基本的默认规则来写
  4. 有足够的耐心和精力
  5. 熟练使用phpstorm,因为这是个规模较大的工程,有个具有代码逻辑分析功能的编辑器,会让你减少不必要的错误,特别是命名空间和引用。如果你还不怎么会用phpstorm,那么先看Be Awesome in PHPStorm
  6. 使用larvel-ide-helper这个插件,不然phpstorm就没有那么智能。(注意生成的_ide_helper.php的版本为L5的)

以下内容部分来自官方文档。由于我建议全部添加命名空间,内容和文档有出入,并有些内容文档未提及

新建L5项目,然后再迁移

新建一个L5项目,新建方法参考这里,然后拷贝L4的文件到新建的项目下面。

拷贝的文件包括:controller, routes, models, Artisan commands, assets, 还有一些你自己添加的类或者资源。

Composer 你的依赖和包

拷贝你添加的所有的composer依赖和包到L5的 composer.json 中,也包括你引用的其他的代码和SDK。 不过需要注意一点就是,你依次去那些针对Laravel开发的包需要到项目主页看看作者是否支持L5或者说准备支持L5,据我所知,目前主流的包基本已支持,因为改动不是特别大。选好支持L5的版本之后, composer update 就好了。

命名空间 Namespace

L4的命名空间是全局的。虽然官方说能不加命名空间就能迁移,但是还是手动给加上吧!不然以后更麻烦了。提醒一下,有这个方法可以修改命名空间的前缀: php artisan app:name Yourproj

如果你的程序中使用了变量作为动态类名,一定要注意在变量中添加完整的命名空间:

# L4中可能存在的写法$myClassName = 'Dog';$obj = new $myClassName(); // 在L5中将要报错# L5中要修改为$myClassName = 'app\\Models\\Dog';$obj = new $myClassName();

配置文件 Configuration

项目根目录命令行 cp .env.example .env ,拷贝你自定义的配置到这里,配置文件不再像之前那样有很多文件夹供你根据环境选择了,L5下只有这一个,意思就是每个不同的环境都需要自己来稍微定制一些。不过每个项目下面可能都是不同的。写好配置文件后记得保存个模板到 .env.example 供其他队友使用。

在 config/ 下面开始使用 env('DB_HOST', 'localhost') 的方式来调用你的配置到对应的数组键下面。

路由 routes

拷贝原来的 routes.php 到 app/Http/routes.php

控制器 controllers

拷贝你的 contollers 到 app/Http/Controllers 下。添加正确的命名空间到每个类上App\Http\Controllers 。记得让你的 BaseController 继承那个抽象类 Controller 。然后挨个查看文件,根据PHPstorm提示进行纠错,主要包括引用类和命名空间的错误。

模型 models

新建文件夹到 app/Models,把原来的 models 全部拷贝过来。首先,添加命名空间 App\Models 。接着是关联到其他model的一些方法,比如 belongTo, hasMany等,第一个参数需要填写完整的命名空间,例如

class User extends Eloquent {    public function phone()    {        // return $this->hasOne('Phone'); 原来这样写的        return $this->hasOne('App\Models\Phone');  // L5需要添加完整命名空间    }}

过滤器 Filters

L5中的中间件 Middleware 是个重头戏,路由 routes.php 中的 ['before' => 'auth']需要替换为['middleware' => 'auth'] 。

同时还要改一下过滤器Filters:

// app/filters.phpRouter::filter('shall-not-pass', function() {    return Redirect::to('shadow');});

改成这样子

// app/Providers/RouteServiceProvider@boot()$router->filter('shall-not-pass', function() {    return \Redirect::to('shadow');});

缓存 Cache

Builder 不再支持 remember 这个方法了,请使用 Cache::remember 对程序改造 。如果使用了 redis,还需要 composer require 'predis/predis' 。

用户认证 Authentication

按照下面的操作对 User model 进行升级。

删除下面的内容

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\RemindableTraitIlluminate\Auth\UserTrait,那么就把他们删掉。

Artisan Commands

直接拷贝你的命令行程序的文件到 app/Console/Cammands 目录,并添加对应命名空间。

接着拷贝 start/artisan.php 内容到 app/Console/Kernel.php 文件的 command 数组中。例如

protected $commands = [    'Laracasts\Console\Commands\ClearHistoryCommand',    'Laracasts\Console\Commands\SignupsReportCommand',    'Laracasts\Console\Commands\WelcomeUserCommand',];

数据迁移 Database Migrations & Seeds

删除L5 database/migrations 中自带的两个数据迁移文件,然后把你自己原来的数据库迁移文件从app/database/migrations 拷贝到 database/migrations 中来。 app/database/seeds 的文件拷贝到database/seeds 中。

这个操作不需要添加命名空间,因为在 composer.json 中已经引入了该目录。

全局的依赖注入绑定 Global IoC Bindings

如果在 start/global.php 中有ioc绑定的话,那就吧他们移动到 app/Providers/AppServiceProvider.php的 register 方法中。同时还需要引入 App facade

视图模板 Views

直接从 app/views 复制到 resources/views 中。

L4中的 {{ }} 对应为L5的 {!! !!} ,而L4中的 {{{ }}} 对应L5的 {{ }} 。需要对应修改一下。

多语言文件 Translation Files

复制 app/lang 到 resources/lang

Public目录

把你的公共资源都直接拷贝过去吧!

测试文件

复制 app/tests 到 tests 目录。

Form 和 HTML 帮助函数

如果你用了 Form 或者 HTML 帮助函数,那么就在 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的参数为Illuminate\Contracts\Pagination\Presenter对象,需要根据需要建立一个继承该接口的类。

消息队列

L5对应的 Beanstalk 包为: "pda/pheanstalk": "~3.0",不再是 "pda/pheanstalk": "~2.1"

总结

相信你按照上面的步骤执行后,你的程序依然报错。因为自己的项目都可能有一些比较 个性 的地方,所以需要多加细心和耐心来完成纠错。

如果你使用了xdebug的断点调试,可能会让你事半功倍。

遇到问题了欢迎来探讨!

最后祝你 level up !^^


0 0
原创粉丝点击