Home > Backend Development > PHP Tutorial > First introduction to laravel5_PHP tutorial

First introduction to laravel5_PHP tutorial

WBOY
Release: 2016-07-13 10:05:56
Original
825 people have browsed it

First introduction to laravel5

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

Directory structure of 4.2:

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.

App Namespace

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.

Requests is an extension of the core class Request. You can extend different Requests classes and add different functions.

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:

Route::controllers([
'auth' => 'AuthAuthController',
'password' => 'AuthPasswordController',
]);

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:

php artisan route:cache

can be easily generated, or you can use

The code is as follows:

php artisan route:clear

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:


# Http/Controllers/Auth/AuthController.php
use AppHttpControllersController;
use IlluminateContractsAuthGuard;
use IlluminateContractsAuthRegistrar;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
class AuthController extends Controller {
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers;
/**
* Create a new authentication controller instance.
*
* @param IlluminateContractsAuthGuard $auth
* @param IlluminateContractsAuthRegistrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware('guest', ['except' => 'getLogout']);
}
}

 

这是一个登陆授权的控制器,我们看 __construct构造函数,利用参数自动注入了一个 "接口实现(参考手册IoC)" 的绑定,我们看下Registrar:

 

代码如下:


use AppUser;
use Validator;
use IlluminateContractsAuthRegistrar as RegistrarContract;
class Registrar implements RegistrarContract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return IlluminateContractsValidationValidator
*/
public function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}

 

提交用户名密码时的处理:

 

代码如下:


public function postRegister(Request $request)
{
$validator = $this->registrar->validator($request->all());
if ($validator->fails())
{
$this->throwValidationException(
$request, $validator
);
}
$this->auth->login($this->registrar->create($request->all()));
return redirect($this->redirectPath());
}

 

可以看到,表单验证的业务逻辑仅仅一行:

 

代码如下:


$validator = $this->registrar->validator($request->all());

 

整个控制器的代码显得干净易读,我们可以把很多通用的业务逻辑封装成service,比不伦不类地直接封装在控制器类好。

模型

models目录不见了,因为不是所有应用都需要用到数据库的,所以laravel5默认不提供该目录可以理解,而且由于提供了 App 这个namespace,所以我们可以自己在 App/ 下创建 Models 目录,其中所有模型类都声名namespace AppModels;即可,只是使用上比以前麻烦一些,需要先use,不过这样也使得项目结构更加清晰,一切类库都在命名空间的组织之下。

时间有限,先写这么多吧。希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/962924.htmlTechArticle初识laravel5 laravel5发布了,目前国内相关资料还比较少,只能先翻墙去官网先看看了,初步总结了一些变化,就想写下来。 目录结构变化...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template