search
HomeDatabaseMysql TutorialLaravel 4.2 升级 Laravel 5.0 攻略

Laravel 4.2 升级 Laravel 5.0 攻略 https://phphub.org/topics/474 以下所指L4为laravel 4.2,L5为laravel 5.0 建议如下情况进行升级 对L4比较了解,同时对L5有基本的认识 想对比L4和L5的差异,快速学习L5 程序的代码写的不乱,按照Laravel的基本的默认规则

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

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

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

配置文件 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等,第一个参数需要填写完整的命名空间,例如

<code><span>class</span> <span>User</span> <span>extends</span> <span>Eloquent</span> <span>{</span>

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

<span>}</span></code>

过滤器 Filters

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

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

<code><span>// app/filters.php
</span><span>Router<span>::</span></span><span>filter<span>(</span></span><span>'shall-not-pass'</span><span>,</span> <span>function</span><span>(</span><span>)</span> <span>{</span>
    <span>return</span> <span>Redirect<span>::</span></span><span>to<span>(</span></span><span>'shadow'</span><span>)</span><span>;</span>
<span>}</span><span>)</span><span>;</span></code>

改成这样子

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

缓存 Cache

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

用户认证 Authentication

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

删除下面的内容

<code><span>use</span> <span>Illuminate<span>\</span>Auth<span>\</span>UserInterface</span><span>;</span>
<span>use</span> <span>Illuminate<span>\</span>Auth<span>\</span>Reminders<span>\</span>RemindableInterface</span><span>;</span></code>

然后添加以下代码:

<code><span>use</span> <span>Illuminate<span>\</span>Auth<span>\</span>Authenticatable</span><span>;</span>
<span>use</span> <span>Illuminate<span>\</span>Auth<span>\</span>Passwords<span>\</span>CanResetPassword</span><span>;</span>
<span>use</span> <span>Illuminate<span>\</span>Contracts<span>\</span>Auth<span>\</span>Authenticatable</span> <span>as</span> AuthenticatableContract<span>;</span>
<span>use</span> <span>Illuminate<span>\</span>Contracts<span>\</span>Auth<span>\</span>CanResetPassword</span> <span>as</span> CanResetPasswordContract<span>;</span></code>

删除 UserInterface 和 RemindableInterface 这两个接口,然后添加 AuthenticatableContract 和CanResetPasswordContract 这两个接口。

添加以下两个 traits 到类里面

<code><span>use</span> <span>Authenticatable</span><span>,</span> CanResetPassword<span>;</span></code>

如果你用到Illuminate\Auth\Reminders\RemindableTraitIlluminate\Auth\UserTrait,那么就把他们删掉。

Artisan Commands

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

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

<code><span>protected</span> <span>$commands</span> <span>=</span> <span>[</span>
    <span>'Laracasts\Console\Commands\ClearHistoryCommand'</span><span>,</span>
    <span>'Laracasts\Console\Commands\SignupsReportCommand'</span><span>,</span>
    <span>'Laracasts\Console\Commands\WelcomeUserCommand'</span><span>,</span>
<span>]</span><span>;</span></code>

数据迁移 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' :

<code><span>'Illuminate\Html\HtmlServiceProvider'</span><span>,</span></code>

接着在 'aliases' 中添加:

<code><span>'Form'</span>      <span>=</span><span>></span> <span>'Illuminate\Html\FormFacade'</span><span>,</span>
<span>'Html'</span>      <span>=</span><span>></span> <span>'Illuminate\Html\HtmlFacade'</span><span>,</span></code>

分页

替换 $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 !^^


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you create and manage user accounts in MySQL?How do you create and manage user accounts in MySQL?Apr 22, 2025 pm 06:05 PM

The steps to create and manage user accounts in MySQL are as follows: 1. Create a user: Use CREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password'; 2. Assign permissions: Use GRANTSELECT, INSERT, UPDATEONmydatabase.TO'newuser'@'localhost'; 3. Fix permission error: Use REVOKEALLPRIVILEGESONmydatabase.FROM'newuser'@'localhost'; then reassign permissions; 4. Optimization permissions: Use SHOWGRA

How does MySQL differ from Oracle?How does MySQL differ from Oracle?Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

What are the disadvantages of using MySQL compared to other relational databases?What are the disadvantages of using MySQL compared to other relational databases?Apr 22, 2025 pm 05:49 PM

The disadvantages of MySQL compared to other relational databases include: 1. Performance issues: You may encounter bottlenecks when processing large-scale data, and PostgreSQL performs better in complex queries and big data processing. 2. Scalability: The horizontal scaling ability is not as good as Google Spanner and Amazon Aurora. 3. Functional limitations: Not as good as PostgreSQL and Oracle in advanced functions, some functions require more custom code and maintenance.

How do you perform a JOIN operation in MySQL?How do you perform a JOIN operation in MySQL?Apr 22, 2025 pm 05:41 PM

MySQL supports four JOIN types: INNERJOIN, LEFTJOIN, RIGHTJOIN and FULLOUTERJOIN. 1.INNERJOIN is used to match rows in two tables and return results that meet the criteria. 2.LEFTJOIN returns all rows in the left table, even if the right table does not match. 3. RIGHTJOIN is opposite to LEFTJOIN and returns all rows in the right table. 4.FULLOUTERJOIN returns all rows in the two tables that meet or do not meet the conditions.

How does MySQL's performance compare to other RDBMS under high load?How does MySQL's performance compare to other RDBMS under high load?Apr 22, 2025 pm 05:37 PM

MySQL's performance under high load has its advantages and disadvantages compared with other RDBMSs. 1) MySQL performs well under high loads through the InnoDB engine and optimization strategies such as indexing, query cache and partition tables. 2) PostgreSQL provides efficient concurrent read and write through the MVCC mechanism, while Oracle and Microsoft SQLServer improve performance through their respective optimization strategies. With reasonable configuration and optimization, MySQL can perform well in high load environments.

Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor