Laravel 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
建议如下情况进行升级
- 对L4比较了解,同时对L5有基本的认识
- 想对比L4和L5的差异,快速学习L5
- 程序的代码写的不乱,按照Laravel的基本的默认规则来写
- 有足够的耐心和精力
- 熟练使用phpstorm,因为这是个规模较大的工程,有个具有代码逻辑分析功能的编辑器,会让你减少不必要的错误,特别是命名空间和引用。如果你还不怎么会用phpstorm,那么先看Be Awesome in PHPStorm
- 使用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\RemindableTrait
和Illuminate\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 !^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.
