The first Knowledge laravel5,laravel5_php tutorial

Source: Internet
Author: User

First knowledge of Laravel5,laravel5


Directory structure changes

Laravel5 the first emphasis is the project directory structure changes, and 4.2 difference is quite big, article by point.

The new directory structure looks like this:

App
Commands
Console
Events
Handlers
Commands
Events
Http
Controllers
Middleware
Requests
kernel.php
routes.php
Providers
Services
Bootstrap
Config
Database
Migrations
Seeds
Public
Package
Resources
Lang
Views
Storage
Cache
Logs
Meta
Sessions
Views
Work
Tests

4.2 of the directory structure:

App
Commands
Config
Controllers
Database
Lang
Models
Start
Storage
Tests
Views
Bootstrap
Public
In contrast, the change is still relatively large, you can see Config,database is moved to the root directory, Lang,views directory is moved to the resources directory, controllers is integrated into the HTTP directory, models directory is missing, There are some additions to the list.

App namespaces

Another change in Laravel5 is that the app directory defaults to a root namespace app, where all directories and classes under the app should be under the namespace, in short, with the PSR4 standard.

HTTP

Laravel5 that the new directory structure is one of the best structures available today, making our development more handy, such as the HTTP directory:

Http
Controllers
Middleware
Requests
kernel.php
routes.php
Middleware is very strange, in fact, it is an upgrade of the original route filter, now do not have to define the filter in filters.php, instead of creating a class in the middleware directory, And kernel.php in the global or optional, the global middleware in each request will be executed, and optional equivalent to the original filter, can be used in the route, can also be used in the controller.

Requests is an extension of the core class request, you can extend the different requests classes and add different functions.

It can be thought that all the processing about HTTP requests is in the HTTP directory, such as the controller is used to accept a request and return it, so it is reasonable to put it in the HTTP directory.

Routing

Routing is not the same as it used to be, but it is important to note that when we specify the controller namespace, the namespace is not an absolute path, but is relative to app\http\controllers, for example:

Copy the Code code as follows:
Route::controllers ([
' Auth ' = ' auth\authcontroller ',
' Password ' = ' Auth\passwordcontroller ',
]);

The corresponding class can be found under the App/http/controllers/auth directory.

In addition, routing supports caching to improve performance through command-line tools

Copy the Code code as follows:
PHP Artisan Route:cache

Can be easily generated, or through

Copy the Code code as follows:
PHP Artisan Route:clear

Cleans up the cache.

Services

We see a service directory under the app directory, which I think is a great idea, and I've always been annoyed with the large segment of business logic code in the controller, and I'd like to encapsulate these business logic in a separate layer, and services can be used to do the job, of course. , it is not necessary, but I strongly recommend to use. Take a look at the demo from Laravel5:

Copy CodeThe code is as follows:
# http/controllers/auth/authcontroller.php
<?php namespace App\http\controllers\auth;
Use App\http\controllers\controller;
Use Illuminate\contracts\auth\guard;
Use Illuminate\contracts\auth\registrar;
Use Illuminate\foundation\auth\authenticatesandregistersusers;
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 \illuminate\contracts\auth\guard $auth
* @param \illuminate\contracts\auth\registrar $registrar
* @return void
*/
Public function __construct (Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;
$this->middleware (' guest ', [' except ' = ' getlogout ']);
}
}

This is a login authorization controller, we look at the __construct constructor, using parameters to automatically inject a "interface implementation (reference manual IOC)" binding, we look at the registrar:

Copy the Code code as follows:
<?php namespace App\services;
Use App\user;
Use Validator;
Use Illuminate\contracts\auth\registrar as registrarcontract;
Class Registrar implements Registrarcontract {
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \illuminate\contracts\validation\validator
*/
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 ' and bcrypt ($data [' Password ']),
]);
}
}

Processing when submitting a user name password:

Copy the Code code as follows:
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 ());
}

As you can see, the business logic for form validation is just one line:

Copy the Code code as follows:
$validator = $this->registrar->validator ($request->all ());

The code for the entire controller looks clean and readable, and we can encapsulate a lot of common business logic into a service that is better than a nondescript directly encapsulated in a controller class.

Model

Models directory is missing, because not all applications need to use the database, so LARAVEL5 does not provide the directory can be understood by default, and because the app is provided this namespace, so we can create their own app/under the Models directory, All of the model classes are well known namespace app\models; it's just a bit more cumbersome to use than before, but it also makes the project structure clearer, with all class libraries under the organization of the namespace.

The time is limited, write so much first. I hope we can enjoy it.

http://www.bkjia.com/PHPjc/963125.html www.bkjia.com true http://www.bkjia.com/PHPjc/963125.html techarticle Initial Knowledge LARAVEL5,LARAVEL5 directory structure changes Laravel5 First emphasis is the project directory structure changes, and 4.2 difference is quite big, article by point. The new directory structure looks like this ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.