laravel5 has been released. At present, there is relatively little domestic related information, so I can only go over the wall and go to the official website to take a look first. I have initially summarized some changes, and I just want to Write it down.
Directory structure changes
The first thing laravel5 emphasizes is the change in the project directory structure. The difference from 4.2 is quite big. Let’s talk about it one by one.
The new directory structure will look like this:
app
Commands
Console
Events
Handlers
Commands
Events
Http
Controllers
Middleware
Requests
Kernel.php
routes.php
Providers
Services
bootstrap
config
database
migrations
public
package
resources
lang
views
storage
cache
logs
meta
sessions
views
work
tests
app
commands
config
controllers
database
lang
models
start
storage
tests
views
bootstrap
public
In comparison, the changes are quite big. You can see that the config and database have been moved to the root directory, the lang and views directories have been moved to the resources directory, the controllers have been integrated into the http directory, the models directory has disappeared, and there are some new additions. The table of contents is omitted.
There is another change in laravel5, that is, the app directory has a root namespace App by default. All directories and classes under App should be in this namespace. In short, the psr4 standard is adopted.
HTTP
Laravel5 believes that the new directory structure is one of the best structures currently, which can make our development more convenient, such as http directory:
Http
Controllers
Middleware
Requests
Kernel.php
routes.php
Middleware is very unfamiliar. In fact, it is an upgraded version of the original routing filter. Now there is no need to define filters in filters.php. Instead, classes are created in the Middleware directory and configured globally or optionally in Kernel.php. The middleware will be executed on every request, and the optional one is equivalent to the original filter, which can be used in routing or in controllers.
It can be considered that all processing related to http requests are in the http directory. For example, the controller is used to accept a request and return it, so it is reasonable to place it in the Http directory.
Routing
The routing is not much different from the previous one, but it should be noted that when we specify the controller namespace, the namespace is not an absolute path, but relative to AppHttpControllers, for example:
The code is as follows:
The corresponding class can be found in the App/Http/Controllers/Auth directory.
In addition, routing also supports caching to improve performance through command line tools
The code is as follows:
can be easily generated, or you can use
The code is as follows:
Clear cache.
Services
We see that there is also a Services directory under the App directory. I think this is a great concept. I have always been annoyed by the presence of large sections of business logic code in the controller. I would like to use one A separate layer encapsulates these business logic, and services can be used to do this work. Of course, it is not necessary, but I strongly recommend it. Let’s take a look at the demo that comes with laravel5:
The code is as follows:
这是一个登陆授权的控制器,我们看 __construct构造函数,利用参数自动注入了一个 "接口实现(参考手册IoC)" 的绑定,我们看下Registrar:
代码如下:
提交用户名密码时的处理:
代码如下:
可以看到,表单验证的业务逻辑仅仅一行:
代码如下:
整个控制器的代码显得干净易读,我们可以把很多通用的业务逻辑封装成service,比不伦不类地直接封装在控制器类好。
模型
models目录不见了,因为不是所有应用都需要用到数据库的,所以laravel5默认不提供该目录可以理解,而且由于提供了 App 这个namespace,所以我们可以自己在 App/ 下创建 Models 目录,其中所有模型类都声名namespace AppModels;即可,只是使用上比以前麻烦一些,需要先use,不过这样也使得项目结构更加清晰,一切类库都在命名空间的组织之下。
时间有限,先写这么多吧。希望大家能够喜欢。