If you've been following Scotch for a while, you heard a lot about Laravel. Some months ago, Laravel 5.3 was released. It introduced new features like Mailables.
如果您一直关注Scotch一段时间,那么您会听到很多有关Laravel的信息 。 几个月前, Laravel 5.3发布了。 它引入了诸如Mailables之类的新功能。
Yesterday, the 25th of January, Laravel 5.4 was released.
昨天,1月25日,Laravel 5.4发布了。
It comes with no surprise that it brought a lot of new and exciting features to Laravel, and for the remainder of this article, we shall go through the new features.
毫不奇怪,它为Laravel带来了许多新的令人兴奋的功能,在本文的其余部分中,我们将介绍这些新功能。
新的中间件 ( New Middlewares )
With the release of 5.4, two new middlewares are now shipped with the framework. If you don't know what middlewares are, check out Understanding Laravel Middlewares.
在5.4版本中,该框架现在附带了两个新的中间件。 如果您不知道什么是中间件,请查看了解Laravel中间件 。
The middlewares are:
中间件是:
修剪字符串中间件 (Trim Strings Middleware)
Just as the name implies, this middleware trims extra spaces from request data. For example, a user submits their email through a form on your website and mistakenly types in some extra space after the email.
顾名思义,该中间件从请求数据中删除了多余的空间。 例如,用户通过您网站上的表单提交电子邮件,并错误地在电子邮件后面输入一些额外的空格。
This middleware will automatically trim whitespace, so if a user submits something like this.
该中间件将自动修剪空白,因此,如果用户提交了这样的内容。
// 'demo@example.org '
Because of the existence of this middleware (\Illuminate\Foundation\Http\Middleware\TrimStrings::class
) in App/Kernel.php, it gets converted into.
由于App / Kernel.php中存在此中间件( \Illuminate\Foundation\Http\Middleware\TrimStrings::class
),因此将其转换为。
// 'demo@example.org'
将空字符串转换为Null (Convert Empty Strings to Null)
Still, as the name implies, this middleware converts empty strings to null. So if a user submits an empty form instead of getting ''
it gets converted to null
.
顾名思义,该中间件仍然将空字符串转换为null。 因此,如果用户提交的是空表单而不是''
则它将转换为null
。
高阶讯息 ( Higher order messages )
This is perhaps one of my favourite feature ever added to Laravel. It much easier to show than explain.
这也许是我曾经为Laravel添加的最喜欢的功能之一。 显示起来比解释容易得多。
Imagine we have a collection of blog posts and we want to perform an operation on each item in the collection. Normally, we would do this.
想象一下,我们有一个博客帖子集合,并且我们想对该集合中的每个项目执行一项操作。 通常,我们会这样做。
Learn about Collections in Laravel.
了解有关Laravel中的集合的信息 。
// Loop through all the posts and schedule them for sharing to twitter
$posts->each(function ($post) {
return $post->schedule(TWITTER);
});
With High order messages in Laravel, we can simplify the above code into this.
借助Laravel中的高阶消息,我们可以将上面的代码简化为此。
$posts->each->schedule(TWITTER);
Yes, this is now a possibility. It doesn't just stop here either, we can take this a bit further and chain them. Here, another scenario.
是的,这现在是可能的。 它也不止于此,我们可以更进一步,将它们链接起来。 在这里,另一种情况。
/**
* Loop through all the posts and reject any archived post
* Then for the remaining posts, schedule each of them to twitter.
*/
$posts->reject(function ($post) {
return $post->archived;
})->each(function ($post) {
return $post->schedule(TWITTER);
});
With high order messages, we can simplify the above code into this.
对于高阶消息,我们可以将上面的代码简化为此代码。
$posts->reject->archived->each->schedule(TWITTER);
Want to see how it's done, check out this commit. This is simply mindblowing
想看看它是如何完成的,签出这个commit 。 这简直令人难以置信

从长生不老药到混合药 ( From Elixir to Mix )

Laravel Elixir为我们提供了一大口的包装,使资产的开发和包装更加容易。
The next version of Laravel Elixir is changing the underlying system and will be built on Webpack, instead of Gulp. This will replace the plugin ecosystem, and because of such a significant change, it was time to rename the package.
Laravel Elixir的下一版本将更改基础系统,并将基于Webpack而非Gulp构建。 这将取代插件生态系统,并且由于发生了如此重大的变化,是时候重命名软件包了。
Also, Laravel Mix comes with a new helper function called mix()
that serves as a replacement to the former elixir()
function.
另外,Laravel Mix附带了一个新的名为mix()
辅助函数,该函数可以替代以前的elixir()
函数。
If you have a project still using Elixir, you can continue using it, as it is still supported and not going anywhere anytime soon.
如果您的项目仍在使用Elixir,则可以继续使用它,因为该项目仍受支持,并且不会在任何地方出现。
流利的路由 ( Fluent Routing )
The Route
facade in Laravel is now fluent. Previously, if we wanted to name a route, we would do this.
Laravel的Route
门面现在很流畅。 以前,如果我们想命名一条路线,我们会这样做。
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
Now, we can do this.
现在,我们可以做到这一点。
Route::name('profile')->get('user/{id}/profile', function ($id) {
// some closure action...
});
We could register a route name and a middleware
我们可以注册一个路由名和一个中间件
Route::name('users.index')->middleware('auth')->get('users', function () {
// some closure action...
});
Registering a middleware with a route prefix and group
使用路由前缀和组注册中间件
Route::middleware('auth')->prefix('api')->group(function () {
// register some routes...
});
Registering a middleware to a resource controller
将中间件注册到资源控制器
Route::middleware('auth')->resource('photo', 'PhotoController');
组件和插槽 ( Components and Slots )
Components and slots allow us to simplify HTML elements into reusable areas.
组件和插槽使我们可以将HTML元素简化为可重用的区域。
In our application, we usually have components like modals, notification panel etc. We can now define them as components and reuse them.
在我们的应用程序中,我们通常具有模态,通知面板等组件。我们现在可以将它们定义为组件并重用它们。
To create a component, we simply create a new blade file and refer to it using.
要创建组件,我们只需创建一个新的刀片文件并使用即可引用它。
@component('path.to.blade.file')
Modal text go in here
@endcomponent
You can read all about components and slots here.
实时门面 ( Real-Time Facades )
Facades provide a “static” interface to classes that are available in the application’s service container. Laravel ships with many facades which provide access to almost all of Laravel’s features. Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods.
外观为应用程序的服务容器中可用的类提供“静态”接口。 Laravel附带了许多外墙,可以访问Laravel的几乎所有功能。 Laravel外观作为服务容器中基础类的“静态代理”,提供了简洁,表达性强的语法的优势,同时保持了比传统静态方法更高的可测试性和灵活性。
One example that comes to mind is Auth
. In laravel, we access an authenticated user's data by doing Auth::user()
. This Auth
class is a facade.
我想到的一个例子是Auth
。 在laravel中,我们通过执行Auth::user()
访问经过身份验证的用户的数据。 Auth
类是一个门面。
Now in Laravel 5.4, we can create facades on the fly. All we need do is namespace class on the Facade namespace and we can use the class as a facade.
现在在Laravel 5.4中,我们可以即时创建外观。 我们需要做的只是Facade名称空间上的名称空间类,我们可以将其用作外观。
namespace Facade\App\User;
Doing this will automatically convert our User
model into a facade.
这样做会自动将我们的User
模型转换为外观。
Laravel黄昏 ( Laravel dusk )

Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your machine. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilise any other Selenium compatible driver you wish.
Laravel Dusk提供了一个表达性强,易于使用的浏览器自动化和测试API。 默认情况下,Dusk不需要您在计算机上安装JDK或Selenium。 相反,Dusk使用独立的ChromeDriver安装。 但是,您可以自由使用所需的任何其他Selenium兼容驱动程序。
Basically, it means we can test our Laravel code like we would in a web browser. Currently, Laravel uses browser-kit for testing.
基本上,这意味着我们可以像在网络浏览器中一样测试Laravel代码。 目前,Laravel使用浏览器套件进行测试。
The problem with browser-kit is that it is limited, and Dusk serves as an alternative with more features.
浏览器工具包的问题在于它的局限性,而黄昏功能则具有更多功能。
Dusk can detect AJAX etc. A full-length article on Dusk is on the way, stay tuned.
Dusk可以检测到AJAX等。有关Dusk的完整文章正在进行中,请继续关注。
修复:指定的密钥太长错误 ( Fix: Specified key was too long error )

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.
Laravel 5.4更改了默认数据库字符集,现在为utf8mb4,其中包括对存储表情符号的支持。 这只会影响新的应用程序,并且只要您运行的是MySQL v5.7.7及更高版本,您就无需执行任何操作。
For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:
对于那些运行MariaDB或更旧版本MySQL的用户,在尝试运行迁移时可能会遇到以下错误:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:
如要解决此问题的《迁移指南》所述,您只需编辑AppServiceProvider.php文件,并在启动方法中设置默认字符串长度:
use Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}
结论 ( Conclusion )
Some features not explained above include
上面没有解释的一些功能包括
- Resourceful Controller with Model Boilerplate: in simple terms, it is route model bindings for controllers. 带模型样板的资源丰富的控制器 :简单来说,它是控制器的路由模型绑定 。
- JSON Based Language Files: this means that translation for text in Laravel can now be done with JSON files instead of PHP files. Learn More. 基于JSON的语言文件 :这意味着Laravel中的文本翻译现在可以使用JSON文件而不是PHP文件完成。 了解更多 。
- Markdown Mailables: this allows us to build Mailables in laravel using the Markdown syntax. Markdown Mailables :这允许我们使用Markdown语法在laravel中构建Mailables 。
- Map Eloquent Events to Dedicated Classes. Here is a video that further explains this. 将口才事件映射到专用类。 这是一个进一步说明这一点的视频 。
If something was omitted, please let us know in the comment. Also, there is a free video series on Laracasts that shows What's New in Laravel 5.4.
如果遗漏了一些内容,请在评论中告知我们。 另外,在Laracasts上有一个免费的视频系列,其中显示了Laravel 5.4的新增功能 。