Laravel:真的干净又优雅吗?

Laravel is a clean and classy framework for PHP web development. Freeing you from spaghetti code, Laravel helps you create wonderful applications using simple, expressive syntax. Development should be a creative experience that you enjoy, not something that is painful. Enjoy the fresh air.

Laravel是一个干净优雅PHP Web开发框架。 Laravel使您摆脱了意大利面条式代码的束缚,可帮助您使用简单的表达语法创建出色的应用程序。 开发应该是您享受的创造性体验,而不是痛苦的事情。 享受新鲜的空气。

That’s the text which can be found on the Laravel homepage and, if we’d believe it, wouldn’t it be wonderful? Let’s test this claim by building a simple TODO application and see how much effort we have to put into it.

那是可以在Laravel主页上找到的文字,如果我们相信这一点,那会不会很美妙? 让我们通过构建一个简单的TODO应用程序来测试此要求,并查看我们需要付出多少努力。

移居 (Migrations)

First we’ll design the database. The application requires a rather simple schema, a single table with 5 columns to store an ID, title, description, and timestamps when the task was created and last updated.

首先,我们将设计数据库。 该应用程序需要一个相当简单的模式,一个包含5列的表格来存储ID,标题,描述和时间戳,这些数据是在创建任务和最后更新任务时存储的。

Laravel has a feature called migrations, which you might be familiar with already if you’ve used other frameworks like Ruby on Rails. Migrations are files which are used to update your database. As they are executed, they change your database schema in such a manner that you can easily apply updates to it.

Laravel具有一项称为迁移的功能,如果您使用了Ruby on Rails等其他框架,则可能已经熟悉。 迁移是用于更新数据库的文件。 在执行它们时,它们以您可以轻松地对其应用更新的方式更改数据库模式。

For our application, the migration looks something like this:

对于我们的应用程序,迁移看起来像这样:

<?php
class Create_Todo_Table
{
public function up() {
Schema::table("todos", function($table) {
$table->create();
$table->increments("id");
$table->string("title", 20);
$table->text("description");
$table->timestamps();
});
}

public function down() {
Schema::drop("todos");
}
}

The up() method is called when the migration is being executed and the down() method is called when the migration is being reverted.

执行迁移时将调用up()方法,而还原迁移时将调用down()方法。

该模型 (The Model)

As Laravel is an MVC framework, we’ll need to create the model. Like with any other MVC framework you might know, the model is the component which is responsible for communicating with the database. Our table is simple, and so it only needs a simple model:

由于Laravel是MVC框架,因此我们需要创建模型。 与您可能知道的任何其他MVC框架一样,模型是负责与数据库进行通信的组件。 我们的表很简单,因此只需要一个简单的模型:

<?php
class Todo extends Eloquent
{
public static $timestamps = true;
}

The class name is “Todo” and Laravel will automatically associate this with the todos table in our database. Eloquent is an ORM class for models that Laravel uses, which provides a smooth way to work with database objects.

类名是“ Todo”,Laravel会自动将其与数据库中的todos表关联。 Eloquent是Laravel使用的模型的ORM类,它提供了一种使用数据库对象的流畅方法。

The $timestamps property is set to true, which means that whenever a new entry is created or an existing entry is updated, the created_at and the updated_at fields will be updated accordingly.

$timestamps属性设置为true,这意味着无论何时创建新条目或更新现有条目,都将相应地更新created_atupdated_at字段。

控制器 (The Controller)

Now let’s create the controller. The controller is where all the business logic is located, and should therefore contain functionality to:

现在让我们创建控制器。 控制器是所有业务逻辑所在的位置,因此应包含以下功能:

  • Retrieve all of the entries in the table to list them

    检索表中的所有条目以列出它们
  • Retrieve the information of a specific entry with a given id

    检索具有给定id的特定条目的信息
  • Delete an entry with a given id

    删除具有给定ID的条目
  • Compose a form to add a new entry

    撰写表格以添加新条目
  • Add the new entry to the database and compose a message to confirm the addition

    将新条目添加到数据库,并编写一条消息以确认添加

This gives us the following controller class with five methods, which we call actions. An action’s declaration must be preceded by the prefix “action_”.

这为我们提供了以下具有五个方法的控制器类,我们称其为动作。 动作的声明必须在前缀“ action_”之前。

<?php
class Todo_Controller extends Base_Controller
{
public function action_list() {
$todos = Todo::all();
return View::make("list")
->with("todos", $todos);
}

public function action_view($id) {
$todo = Todo::where_id($id)->first();
return View::make("view")
->with("todo", $todo);
}

public function action_delete($id) {
$todo = Todo::where_id($id)->first();
$todo->delete();
return View::make("deleted");
}

public function action_new() {
return View::make("add");
}

public function action_add() {
$todo = new Todo();
$todo->title = Input::get("title");
$todo->description = Input::get("description");
$todo->save();
return View::make("success");
}
}

The code is very straightforward. The first method action_list() will get all of the entries in the database. This is where Eloquent makes things easy, requiring only Todo::all() to fetch them. Then it returns a view with all of the entries from the database bound to the $todos variable.

该代码非常简单。 第一个方法action_list()将获取数据库中的所有条目。 这是Eloquent使事情变得容易的地方,只需要Todo :: all()即可获取它们。 然后,它返回一个视图,其中数据库中的所有条目都绑定到$todos变量。

The other methods are also easy to read. They mostly exist to manipulate a database object and return a view with some data bound to it. The last method might be an exception; the action_add() will be called when submitting the form to add a new TODO entry. Input::get() is used to retrieve the submitted form values.

其他方法也很容易阅读。 它们大多数用于操纵数据库对象并返回绑定了一些数据的视图。 最后一个方法可能是一个例外。 提交表单以添加新的TODO条目时,将调用action_add()Input::get()用于检索提交的表单值。

风景 (The View)

Now we’ve come to the view. Laravel uses its own Blade templating engine which gives us clean and readable code. I will give an example of the first view, the list. With the make/with statement under the return of the view from action_list(), we put all the results of Todo::all() into $todos. We can now use that in the view:

现在我们来看一下。 Laravel使用自己的Blade模板引擎,为我们提供了清晰易读的代码。 我将举例说明第一个视图,即列表。 在从action_list()返回视图的状态下使用make / with语句,将Todo::all()所有结果放入$todos 。 现在,我们可以在视图中使用它:

<h2>Todo list</h2>
<p>{{ HTML::link_to_action("todo@new", "Add new todo") }}</p>
<ul>
@foreach($todos as $todo)
<li>{{ HTML::link_to_action("todo@view", $todo->title, array($todo->id)) }} - {{ HTML::link_to_action("todo@delete", "Delete", array($todo->id)) }}</li>
@endforeach
</ul>

That’s all it takes! First we let Blade create a link to the controller’s action action_new(). Then we see the foreach statement which is quite similar to the one native to PHP. For every task we create a link to view the entry. The link text is the second parameter, $todo->title, and any parameters for the action should be provided next. We need the ID of the entry we want to view, which $todo->id provides. We also create a link to delete the task, again including the ID of the TODO as parameter.

这就是全部! 首先,我们让Blade创建到控制器动作action_new()的链接。 然后我们看到foreach语句,该语句与PHP的本机语句非常相似。 对于每个任务,我们都创建一个链接以查看条目。 链接文本是第二个参数$todo->title ,接下来应提供该操作的所有参数。 我们需要我们要查看的条目的ID,该条目由$todo->id提供。 我们还创建了一个删除任务的链接,同样包含TODO的ID作为参数。

结论 (Conclusion)

So, that was that, and indeed it is simple to make an application using Laravel. The code is straightforward and easily readable. I hope this was useful to you, and you consider using Laravel with your next PHP application. See you next time!

就是这样,使用Laravel创建应用程序确实很简单。 该代码简单易懂。 我希望这对您有用,并且您考虑在下一个PHP应用程序中使用Laravel。 下次见!

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/laravel-really-clean-and-classy/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值