Laravel 4的新功能

tech2023-10-27  93

Laravel is a PHP framework which uses the MVC pattern. Of course, there are many such projects and I’m not going to compare Laravel with other frameworks in this article. Instead, I’m going to share with you what’s new in the newest version of Laravel – Laravel 4.

Laravel是一个使用MVC模式PHP框架。 当然,有许多这样的项目,在本文中我不会将Laravel与其他框架进行比较。 相反,我将与您分享最新版本的Laravel – Laravel 4的新功能。

去耦组件 (Decoupled Components)

Laravel 4 does not contain very much code as you can see on its GitHub page. In fact, the app project just registers services it uses. This is the biggest change in Laravel 4; instead of having all of the code packed in one repository, the functionality is separated in so called Illuminate Components. The app project just uses Composer to manage the dependencies that we are familiar with from the retro Laravel: the Controller/Routing system, the Object Relational Mapper named Eloquent, and view management with the Blade templating engine. They are now all organized as separated components and are decoupled using service locators.

Laravel 4不包含太多代码,正如您在其GitHub页面上所见。 实际上,该应用程序项目只是注册其使用的服务。 这是Laravel 4中最大的变化。 并非将所有代码打包在一个存储库中,而是在所谓的Illuminate组件中分离了功能。 该应用程序项目仅使用Composer来管理我们熟悉的Laravel依赖项:控制器/路由系统,名为Eloquent的对象关系映射器,以及使用Blade模板引擎的视图管理。 现在,它们都被组织为独立的组件,并使用服务定位器解耦。

Maybe you’re not impressed, and it’s possible that you’re asking yourself: “Wow, is that it? What’s the big deal?” The big deal is that, with true decoupling, we can pick out the Illuminate Components we need and use them in our own project. So, you’re just looking for a nice ORM, but you don’t want the routing system, the views, the pagination and the mail component? No problem, you can use what you want. You just take what you need and not the whole ‘robust’ framework.

也许您没有留下深刻的印象,并且您可能在问自己:“哇,是吗? 有什么大不了的?” 重要的是,通过真正的去耦,我们可以挑选出所需的Illuminate组件,并在我们自己的项目中使用它们。 因此,您只是在寻找一个不错的ORM,但是您不想要路由系统,视图,分页和邮件组件吗? 没问题,您可以使用所需的东西。 您只需要您需要的东西,而不是整个“健壮”的框架。

创建一个“普通”项目 (Creating a “Normal” Project)

For the users who want to use the complete framework as always, just retrieve this repository: github.com/illuminate/app.git. After that, install Composer and go to the directory where the code is and run composer update. This will get you all of the Laravel dependencies. Inside the app directory, everything is similar to the old Laravel – the directory structure and the files – so it won’t take much effort to update your Laravel 3 projects to Laravel 4.

对于想要始终使用完整框架的用户,只需检索以下存储库: github.com/illuminate/app.git 。 之后, 安装Composer并转到代码所在的目录,然后运行composer update 。 这将为您提供所有的Laravel依赖项。 在app目录中,所有内容都与旧的Laravel相似(目录结构和文件),因此无需花费太多精力即可将Laravel 3项目更新为Laravel 4。

路线 (Routes)

Just like before, routes are defined in the routes.php file. You can return anything you want, although note that, for controllers, Laravel doesn’t use magic renaming anymore. In Laravel 3 you could call a controller method like this:

和以前一样,路由是在routes.php文件中定义的。 您可以返回任何您想要的东西,尽管请注意,对于控制器,Laravel不再使用魔术重命名。 在Laravel 3中,您可以像这样调用控制器方法:

<?php Route::get('/', 'home@index');

The controller went by the name of HomeController. But now you have to use:

控制器的名称为HomeController 。 但是现在您必须使用:

<?php Route::get('/', 'HomeController@index');

While this might not seem like a feature enhancement, it really is. For example, if you use the route component as stand-alone library, you aren’t bound to Laravel’s naming conventions.

虽然这似乎不像功能增强,但实际上确实如此。 例如,如果您将route组件用作独立库,则不必受限于Laravel的命名约定。

通过测试改进代码 (Better Code through Testing)

Critics of previous versions of Laravel argued its code would never be able to achieve high quality status. By using a lot of static methods for to produce nice and clean code, the developers had to sacrifice the project’s testability. The main developer (Taylor Otwell) did a great job balancing easy code and testability, but it’s true the code could never be fully tested.

Laravel先前版本的批评者认为,其代码永远无法达到高质量的状态。 通过使用许多静态方法来生成精美的代码,开发人员不得不牺牲项目的可测试性。 主要开发人员(Taylor Otwell)在平衡简单代码和可测试性方面做得很好,但是确实不能对代码进行完全测试。

That era ends now. By using an IoC container for registering all of the components, everything can be fully tested. Each component has tests for most of its features, and with each commit those tests are validated by Travis CI. To preserve a clean way of calling components, Laravel uses the façade pattern.

那个时代现在结束了。 通过使用IoC容器注册所有组件,可以对所有组件进行全面测试。 每个组件都针对其大多数功能进行了测试,并且每次提交时,这些测试都会由Travis CI进行验证。 为了保持调用组件的简洁方式,Laravel使用了façade模式 。

容器及其绑定 (The Container and Its Bindings)

When you create a Laravel project, you use an IoC container to resolve all of the dependencies. The Application class is extended from IlluminateContainer. While you use facades to call component methods, you can still bind things to the container to make them available throughout your whole application. For example:

创建Laravel项目时,可以使用IoC容器来解析所有依赖项。 Application类是从IlluminateContainer扩展的。 当您使用Facades调用组件方法时,您仍然可以将事物绑定到容器以使它们在整个应用程序中可用。 例如:

<?php App::bind('hello', function() { return "Hello to you, sir"; }); Route::get('/', function() { return App::make('hello'); });

Here we’ve bound a function that returned the string “Hello to you, sir” to ‘hello’ in the container.

在这里,我们绑定了一个函数,该函数在容器中将字符串“ Hello to you,sir”返回给“ hello”。

The container also allows us to lazily bind objects without looking at its constructor arguments. This can be done through Reflection. For example:

容器还允许我们懒惰地绑定对象,而无需查看其构造函数参数。 这可以通过反射来完成。 例如:

<?php Class writer { public function write() { return 'A good day to you, sir'; } } Class helloWorld { protected $writer; public function __construct (Writer $writer) { $this->writer = $writer; } public function write() { return $this->writer->write(); } } Route::get('/', function() { return App::make('helloWorld')->write(); });

You can see that Laravel automatically resolves our dependencies by creating a new Writer class and uses it as argument in the helloWorld class constructor. If you want a specific writer instance used as the argument, you can also bind that object to the container using the approach shown in the previous example.

您可以看到Laravel通过创建一个新的Writer类自动解决了我们的依赖关系,并将其用作helloWorld类构造函数中的参数。 如果要使用特定的writer实例作为参数,则还可以使用上一示例中所示的方法将该对象绑定到容器。

改进的CLI (Improved CLI)

Laravel’s famous CLI, known By the name Artisan, has received some major improvements as well. Using the command line component from Symfony, Artisan now exhibits more friendly behavior. For example, you can get all the available commands just by executing artisan list, and you can get extra information on the commands and what it does.

Laravel著名的CLI(以Artisan为名)也得到了一些重大改进。 使用Symfony的命令行组件,Artisan现在表现出更友好的行为。 例如,仅通过执行artisan list即可获取所有可用命令,并且可以获得有关命令及其功能的更多信息。

新数据库功能 (New Database Features)

For the database component, which most Laravel users will use in their projects, the most notable update is database seeding. If you go to your app/database directory, you’ll see a directory named seeds there.

对于大多数Laravel用户将在其项目中使用的数据库组件,最值得注意的更新是数据库种子。 如果转到您的app/database目录,您将在其中看到一个名为seeds的目录。

To show you this feature, we’ll first need to have a test database table to work on. So, set your connection details in app/config/database.php, navigate to the root directory (above app) and execute php artisan migrate:install to install the migration tables. Then, run php artisan migrate:make create_comments_table --create="comments" which creates a test table named comments.

为了向您展示此功能,我们首先需要有一个测试数据库表才能使用。 因此,在app/config/database.php设置连接详细信息,导航到根目录(在app之上)并执行php artisan migrate:install以安装迁移表。 然后,运行php artisan migrate:make create_comments_table --create="comments"创建一个名为comments的测试表。

Look in the app/database/migrations directory and you’ll see your migration there. We can use the schema builder so that we don’t have to use raw SQL queries. In the up() method, fill this in:

查看app/database/migrations目录,您将在其中看到您的迁移。 我们可以使用架构生成器,这样就不必使用原始SQL查询。 在up()方法中,填写:

<?php public function up() { Schema::create('comments', function($table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->integer('author'); }); }

And in the down() method, put:

然后在down()方法中,输入:

<?php Public function down() { Schema::drop('comments'); }

Save your file and run php artisan migrate and you should receive a confirmation message that it succeeded.

保存您的文件并运行php artisan migrate ,您应该收到确认成功的确认消息。

Now let’s make the seed. A seed is some sort of test data you want to insert in your database for testing your application. Create a new file named comments.php in the database/seeds directory with the following content:

现在让我们播下种子。 种子是要插入数据库以测试应用程序的某种测试数据。 创建一个名为新文件comments.php在database/seed s的以下内容目录:

<?php return [ [ 'title' => 'hello there', 'body' => 'I wish you a warm welcome', 'author' => '5' ], [ 'title' => 'Another hello', 'body' => 'I want to greet you another time', 'author' => '1' ] ];

The code presents a multidimensional array where every member array is a record. Now execute php artisan db:seed. Now your database is seeded (contains the test data)! You can do this every time you do a migration.

该代码提供了一个多维数组,其中每个成员数组都是一条记录。 现在执行php artisan db:seed 。 现在,您的数据库已经播种(包含测试数据)! 您可以在每次迁移时执行此操作。

使用独立组件 (Using Stand-Alone Components)

The biggest update to the new framework, as I said earlier, is the availability of separate components. Laravel uses an instance of IlluminateFoundationApplication as an IoC container so we don’t have to worry about the dependencies, and we have the ability to use our own custom components. That last can be edited in app.php in your config directory.

正如我之前所说,对新框架的最大更新是独立组件的可用性。 Laravel使用IlluminateFoundationApplication的实例作为IoC容器,因此我们不必担心依赖关系,并且可以使用自己的自定义组件。 最后一个可以在config目录中的app.php中进行编辑。

In the future I’m sure there will be many projects available for wrapping Laravel components and to use them in your project. One example is Capsule, which provides us some methods for accessing the components. Right now only the database component is supported, but this will change in the future. For example, a database connection can be established by following code:

将来,我确信会有很多项目可用于包装Laravel组件并在您的项目中使用它们。 一个例子是Capsule ,它为我们提供了一些访问组件的方法。 目前仅支持数据库组件,但是将来会改变。 例如,可以通过以下代码建立数据库连接:

<?php CapsuleDatabaseConnection::make('main', [ //database credentials ], true);

Now you can execute fluent queries by using the static methods of the CapsuleDB class. For documentation, look up at the project on GitHub.

现在,您可以使用CapsuleDB类的静态方法执行流利的查询 。 有关文档,请查看GitHub上的项目。

做得好! (Good Job!)

I think the Laravel developers did a great job again contributing some major improvements to the framework. Testability isn’t a problem any more, and neither is decoupling. Laravel 4 very well may be one of the greatest (if not THE greatest) PHP frameworks currently available thanks to the hard work of all the developers.

我认为Laravel开发人员再次做了出色的工作,为框架做出了一些重大改进。 可测试性不再是问题,也不是分离。 由于所有开发人员的辛勤工作,Laravel 4可能是当前可用的最大(如果不是最大的)PHP框架之一。

翻译自: https://www.sitepoint.com/whats-new-in-laravel-4/

相关资源:jdk-8u281-windows-x64.exe
最新回复(0)