Table of Contents
First introduction to laravel5
Home Backend Development PHP Tutorial First introduction to laravel5_PHP tutorial

First introduction to laravel5_PHP tutorial

Jul 13, 2016 am 10:05 AM
First acquaintance release domestic Compare at present Related net material

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发布了,目前国内相关资料还比较少,只能先翻墙去官网先看看了,初步总结了一些变化,就想写下来。 目录结构变化...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to publish works on Xiaohongshu How to publish articles and pictures on Xiaohongshu How to publish works on Xiaohongshu How to publish articles and pictures on Xiaohongshu Mar 22, 2024 pm 09:21 PM

You can view various contents on Xiaohongshu, which can provide you with various help and help you discover a better life. If you have anything you want to share, you can post it here so that everyone can take a look. , and at the same time, it can bring you profits. It is very cost-effective. If you don’t know how to publish your works here, you can check out the tutorial. You can use this software every day and publish various contents to help everyone use it better. Don’t miss it if you need it! 1. Open Xiaohongshu and click the plus icon below. 2. There are [Video] [Picture] [Live Picture] options here; select the content you want to publish and click to check. 3. Select [Next] on the content editing page. 4. Enter the text content you want to publish and click [Publish Pen]

Why can't Xiaohongshu publish videos of works? How does it publish its work? Why can't Xiaohongshu publish videos of works? How does it publish its work? Mar 21, 2024 pm 06:36 PM

With the rapid development of social media, short video platforms have become the main channel for many users to express themselves and share their lives. Many users may encounter various problems when publishing videos of their works on Xiaohongshu. This article will discuss the reasons that may cause the video publishing of Xiaohongshu works to fail and provide the correct publishing method. 1. Why can’t Xiaohongshu publish videos of works? The Xiaohongshu platform may occasionally experience system failures, which may be caused by system maintenance or upgrades. In this case, users may encounter the problem of being unable to publish videos of their works. Users need to wait patiently for the platform to return to normal before trying to publish. An unstable or slow network connection may prevent users from posting videos of their work on Xiaohongshu. Users should confirm their network environment to ensure that the connection is stable and

Why can't Xiaohongshu be released? What should I do if the content published by Xiaohongshu cannot be displayed? Why can't Xiaohongshu be released? What should I do if the content published by Xiaohongshu cannot be displayed? Mar 21, 2024 pm 07:47 PM

As a lifestyle sharing platform, Xiaohongshu has attracted a large number of users to share their daily life and grow products. Many users have reported that their published content cannot be displayed. What is going on? This article will analyze the possible reasons why Xiaohongshu cannot be released and provide solutions. 1. Why can’t Xiaohongshu be released? Xiaohongshu implements strict community guidelines and has zero tolerance for publishing advertisements, spam, vulgar content, etc. If the user's content violates the regulations, the system will block it and the content will not be displayed. Xiaohongshu requires users to publish high-quality and valuable content, and the content needs to be unique and innovative. If the content is too generic and lacks innovation, it may not pass review and therefore not be displayed on the platform. 3. Account abnormality

When is the best time to publish Xiaohongshu? Where does it post the most traffic recommendations from? When is the best time to publish Xiaohongshu? Where does it post the most traffic recommendations from? Mar 21, 2024 pm 08:11 PM

In today's social network era, Xiaohongshu has become an important platform for young people to share their lives and obtain information. Many users hope to attract more attention and traffic by publishing content on Xiaohongshu. So, when is the best time to post content? This article will explore in detail the selection of Xiaohongshu’s publishing time and the publishing location with the most traffic recommendations. 1. When is the best time to publish Xiaohongshu? The best time to publish content on Xiaohongshu is usually during periods of high user activity. According to the characteristics and behavioral habits of Xiaohongshu users, there are several time periods that are more appropriate. During the time period from 7 pm to 9 pm, most users have returned home from get off work and started browsing content on their mobile phones in search of relaxation and entertainment. Therefore, content posted during this period is more likely to attract users

How to delete Xiaohongshu releases? How to recover after deletion? How to delete Xiaohongshu releases? How to recover after deletion? Mar 21, 2024 pm 05:10 PM

As a popular social e-commerce platform, Xiaohongshu has attracted a large number of users to share their daily life and shopping experiences. Sometimes we may inadvertently publish some inappropriate content, which needs to be deleted in time to better maintain our personal image or comply with platform regulations. 1. How to delete Xiaohongshu releases? 1. Log in to your Xiaohongshu account and enter your personal homepage. 2. At the bottom of the personal homepage, find the "My Creations" option and click to enter. 3. On the "My Creations" page, you can see all published content, including notes, videos, etc. 4. Find the content that needs to be deleted and click the "..." button on the right. 5. In the pop-up menu, select the "Delete" option. 6. After confirming the deletion, the content will disappear from your personal homepage and public page.

How to publish works on Xiaohongshu app? Tutorial on publishing works on Xiaohongshu app in five minutes How to publish works on Xiaohongshu app? Tutorial on publishing works on Xiaohongshu app in five minutes Mar 12, 2024 pm 05:10 PM

How does the Xiaohongshu app publish works? Many friends know that there are a large number of creative works and a strong dating circle in this software. For users who are new to this software, they probably don’t know how to publish their works, so that more people can watch the other side of you. If you still don’t know how to publish the works in it, then quickly refer to the five-minute tutorial on publishing works on the Xiaohongshu app recommended by the editor of this site. Tutorial on publishing works in Xiaohongshu app in five minutes 1. Click [Three] As shown in the picture, click [Three] pointed by the red arrow in the upper left corner. 2. Click [Creation Center] As shown in the picture, click [Creation Center] pointed by the red arrow. 3. Click [Go to Publish] as shown in the picture,

How to publish Xiaohongshu video works? What should I pay attention to when posting videos? How to publish Xiaohongshu video works? What should I pay attention to when posting videos? Mar 23, 2024 pm 08:50 PM

With the rise of short video platforms, Xiaohongshu has become a platform for many people to share their lives, express themselves, and gain traffic. On this platform, publishing video works is a very popular way of interaction. So, how to publish Xiaohongshu video works? 1. How to publish Xiaohongshu video works? First, make sure you have a video content ready to share. You can use your mobile phone or other camera equipment to shoot, but you need to pay attention to the image quality and sound clarity. 2. Edit the video: In order to make the work more attractive, you can edit the video. You can use professional video editing software, such as Douyin, Kuaishou, etc., to add filters, music, subtitles and other elements. 3. Choose a cover: The cover is the key to attracting users to click. Choose a clear and interesting picture as the cover to attract users to click on it.

How to publish Xiaohongshu content? How to delete content posted on Xiaohongshu? How to publish Xiaohongshu content? How to delete content posted on Xiaohongshu? Mar 21, 2024 pm 04:10 PM

With the continuous development of social media, Xiaohongshu has become an important platform for young people to share their lives, discover trends, and obtain inspiration. In this vibrant community, how to publish high-quality content and attract more attention and likes has become a concern for many users. 1. How to publish Xiaohongshu content? Before creating, it is crucial to choose a suitable topic. You can pick an engaging topic based on your interests and expertise. Doing so will not only keep you passionate about the writing process, but it will also make it easier for readers to relate to your work. 2. Carefully design the title: The title is the key to attracting readers to click. It should be concise and clear, and at the same time, it should be attractive and engaging. Avoid using exaggerated wording that may cause the reader to react

See all articles