Laravel 4到Laravel 5-简单升级指南

Laravel 5 is already out, but the fear of change is taking everyone. We keep hearing people complaining about some radical changes. Like, why this new folder structure? Will my application break if I do a composer update?

Laravel 5已经发布,但是对改变的恐惧正在吸引所有人。 我们不断听到人们抱怨一些重大变化。 喜欢,为什么要使用这种新的文件夹结构? 如果我进行composer update我的应用程序会中断吗?

In this article, we’re going to look at how to migrate your existing Laravel 4 application to Laravel 5 and understand the new folder structure.

在本文中,我们将研究如何将现有的Laravel 4应用程序迁移到Laravel 5,并了解新的文件夹结构。

Laravel Logo

安装 (Installation)

My existing Laravel 4 application is a demo from a previous article about using Google Analytics API. The application doesn’t have too much code, but it will do the job for our tutorial.

我现有的Laravel 4应用程序是上一篇有关使用Google Analytics(分析)API的文章的演示 。 该应用程序没有太多代码,但可以完成我们的教程。

Let’s first install Laravel 5 on our computer and create a temporary folder to hold our Laravel 4 version of the application.

首先,我们在计算机上安装Laravel 5,并创建一个临时文件夹来保存应用程序的Laravel 4版本。

composer create-project laravel/laravel --prefer-dist

I prefer to install Laravel via composer, but you can visit the documentation to read more about the Laravel installer.

我更喜欢通过composer安装Laravel,但是您可以访问文档以了解有关Laravel安装程序的更多信息。

You can use the Vagrant box from the repository, or use Homestead Improved. If everything goes well, you should see the Laravel 5 welcome page.

您可以使用存储库中的Vagrant框,也可以使用Homestead改良版 。 如果一切顺利,您应该会看到Laravel 5欢迎页面。

配置文件 (Configuration Files)

The old app/config folder now lives under the root of your application, thus we have to move our app/config/analytics.php to config/analytics.php. The credentials are pasted directly to the file, so why not use environment variables.

现在,旧的app/config文件夹位于应用程序的根目录下,因此我们必须将app/config/analytics.php移至config/analytics.php 。 凭据直接粘贴到文件中,所以为什么不使用环境变量。

// config/analytics.php

return [
  'app_name'          => env('app_name'),
  'client_id'         => env('client_id'),
  'client_secret'     => env('client_secret'),
  'api_key'           => env('api_key')
];
// .env

app_name='YOUR APP NAME'
client_id='YOUR CLIENT ID'
client_secret='CLIENT SECRET'
api_key='API KEY'

The .env file is automatically loaded and can be used to separate your local environment configuration from production, tests, etc.

.env文件会自动加载,可用于将本地环境配置与生产,测试等分开。

路由 (Routing)

Laravel 4 routes are registered inside app/routes.php. In Laravel 5, everything that is HTTP related is grouped under the app/Http folder, including routes, so let’s move our app/routes.php to app/Http/routes.php.

Laravel 4条路线在app/routes.php routes.php中注册。 在Laravel 5中,所有与HTTP相关的内容都分组在app/Http文件夹下,包括路由,因此让我们将app/routes.php移至app/Http/routes.php

筛选器 (Filters)

Laravel 5 has moved from filters to middlewares, so if you have any filters inside your routes, make sure you change to middlewares.

Laravel 5已从过滤器转移到中间件 ,因此,如果您的路由内有任何过滤器,请确保更改为中间件。

Route::get(‘/report’, [‘middleware’ => ‘auth’, function()
{
    //
}]);

If you have a custom filter, you can migrate it to be a middleware. I have a GoogleLogin middleware that I’m using in my routes, the implementation will be like the following.

如果您有自定义过滤器,则可以将其迁移为中间件。 我在自己的路线中使用了GoogleLogin中间件, GoogleLogin如下所示。

// app/Http/Middleware/GoogleLogin.php

class GoogleLogin
{
  public function handle($request, Closure $next)
  {
    $ga = \App::make('\App\Services\GoogleLogin');
    if (!$ga->isLoggedIn()) {
      return redirect('login');
    }

    return $next($request);
  }

}
// app/Http/Kernel.php

protected $routeMiddleware = [
        'google_login'  => 'App\Http\Middleware\GoogleLogin',
];
// app/Http/routes.php

Route::any('/search', ['middleware' => 'google_login', 'as' => 'search', 'uses' => 'GoogleController@search']);

The CRSF protection middleware is added by default. If you want to remove it, you can go to the app/Http/Kernel.php file and comment out the proper line.

默认情况下添加CRSF保护中间件。 如果要删除它,可以转到app/Http/Kernel.php文件并注释掉正确的行。

控制器 (Controllers)

Because our controllers are considered part of the Http logic, we need to move our app/controllers/* to app/Http/Controllers and namespace them with App\Http\Controllers. One last problem you need to fix is to change the BaseController to the Controller class.

因为我们的控制器被视为Http逻辑的一部分,所以我们需要将app/controllers/*移至app/Http/Controllers并使用App\Http\Controllers对其命名空间。 您需要解决的最后一个问题是将BaseController更改为Controller类。

If you don’t like the App root namespace, you can change it globally using the following artisan command.

如果您不喜欢App根名称空间,则可以使用以下artisan命令对其进行全局更改。

php artisan app:name MyApp

移居 (Migrations)

Our Google Analytics application doesn’t have any local database interactions, but the upgrade process is worth mentioning.

我们的Google Analytics(分析)应用程序没有任何本地数据库交互,但是升级过程值得一提。

The app/database directory now lives inside the /database folder, and you only need to move your file there. The directory already contains a users and a password_resets table that you may want to delete or update depending on your needs.

现在, app/database目录位于/database文件夹中,您只需要将文件移动到该目录中。 该目录已经包含一个users和一个password_resets表,您可能需要根据需要删除或更新该表。

楷模 (Models)

The models folder from Laravel 4 is gone, and Laravel 5 puts the User model directly inside the app folder as an example. You can copy your models in there too, and namespace them with App.

Laravel 4中的models文件夹不见了,Laravel 5将User模型直接放在app文件夹中作为示例。 您也可以在其中复制模型,并使用App命名它们。

However, if you don’t like the idea of having your models there, you can create a new folder under the app directory called Models, but don’t forget to namespace your classes with the App\Models namespace.

但是,如果您不喜欢在其中放置模型的想法,则可以在app目录下创建一个名为Models的新文件夹,但是不要忘记使用App\Models命名空间为您的类命名。

namespace App\Models;

class User extends Eloquent {
	…
}

应用服务 (Application Services)

Our src folder contains a GA_Service and a GA_Utils class. If we consider them to be services, we can put them inside app/Services. Otherwise, we can create a new folder called app/GA where we’re going to store our service classes. This will cause a problem since we didn’t use PSR-4 autoloading at first, so we need to update our class references inside the controller with the proper new namespace.

我们的src文件夹包含GA_ServiceGA_Utils类。 如果我们将它们视为服务,则可以将其放入app/Services 。 否则,我们可以创建一个名为app/GA的新文件夹,其中将存储我们的服务类。 这将导致问题,因为我们最初没有使用PSR-4自动加载功能,因此我们需要使用适当的新名称空间更新控制器内部的类引用。

观看次数 (Views)

The application views are moved from the app/views folder to the resources/views folder.

应用程序视图从app/views文件夹移动到resources/views文件夹。

The resources folder also contain the lang folder for your application’s localization, and an assets folder for your front-end assets. Laravel 5 introduced Elixir which adapts the Gulp task runner to the Laravel development environment.

resources文件夹还包含用于应用程序本地化的lang文件夹和用于前端assets文件夹。 Laravel 5引入了Elixir ,它使Gulp任务运行程序适应Laravel开发环境。

作曲家 (Composer)

Make sure you copy your application’s composer dependencies and do any necessary upgrades. For our demo, I’m going to move my "google/apiclient": "1.1.*" to the new composer.json and do a composer updateto reflect the changes.

确保复制应用程序的作曲者依赖项并进行任何必要的升级。 对于我们的演示,我将把"google/apiclient": "1.1.*"到新的composer.json并进行composer update以反映所做的更改。

表格和HTML (Forms and HTML)

The illuminate/html package is removed from the default installation on Laravel 5 and you will need to install it separately.

Laravel 5的默认安装中已删除illuminate/html软件包,您需要单独安装。

To bring back the HTML helpers to your project, you need to add the "illuminate/html": "5.0.*" package to your composer.json and run composer update, then you need to add the 'Illuminate\Html\HtmlServiceProvider' to your config/app.php providers array. If you would like to use the Html and Form facades inside your blade templates, you can append the following facades to your config/app.php facades array.

要将HTML帮助程序带回您的项目,您需要在composer.json添加"illuminate/html": "5.0.*"包并运行composer update ,然后需要添加'Illuminate\Html\HtmlServiceProvider'到您的config/app.php提供者数组。 如果您想在刀片模板中使用HtmlForm门面,则可以将以下门面附加到config/app.php门面数组中。

// config/app.php

'aliases' => [
 
 
	'Form'=> 'Illuminate\Html\FormFacade', 
	'HTML'=> 'Illuminate\Html\HtmlFacade',
],

结论 (Conclusion)

The complexity and duration of the process of upgrading to Laravel 5 always depends on your application’s size, and maybe the process will be much longer than this example for your particular case. In this article, we tried to explain the common process which should take care of most if not all things that need changing.

升级到Laravel 5的过程的复杂性和持续时间始终取决于您应用程序的大小,对于您的特定情况,此过程可能比此示例要长得多。 在本文中,我们试图解释通用流程,该流程应处理大多数(即使不是全部)需要更改的事情。

You are not forced to upgrade to the new folder structure, and you can keep the old one and only update your composer dependencies, but this is not the recommended way of doing it. If you have any questions or comments, make sure to post them below. For more information, see the full version upgrade guide.

您不必强制升级到新的文件夹结构,并且可以保留旧的文件夹结构,而仅更新您的作曲家依赖关系,但这不是推荐的方式。 如果您有任何疑问或意见,请确保将其张贴在下面。 有关更多信息,请参阅完整版本升级指南

翻译自: https://www.sitepoint.com/laravel-4-laravel-5-simple-upgrade-guide/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值