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
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:
'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:
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:
# 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,不过这样也使得项目结构更加清晰,一切类库都在命名空间的组织之下。
时间有限,先写这么多吧。希望大家能够喜欢。

PHP8.1发布:引入curl多个请求并发处理近日,PHP官方发布了最新版本的PHP8.1,其中引入了一个重要的特性:curl多个请求并发处理。这个新特性为开发者提供了一个更加高效和灵活的方式来处理多个HTTP请求,极大地提升了性能和用户体验。在以往的版本中,处理多个请求往往需要通过创建多个curl资源,并使用循环来分别发送和接收数据。这种方式虽然能够实现目

【CNMO新闻】北京时间10月13日14点,雷鸟科技正式召开了雷鸟创新新品发布会。在此次发布会上,雷鸟为消费者带来了消费级真AR眼镜——雷鸟X2,新品售价4999元,首发用户可享4499元,而所有此前购买过雷鸟产品或者类似友商产品的用户,则可享受3999元的优惠价。雷鸟X2采用无线一体化设计,能够像普通眼镜那般,不会遮挡正常眼神。新眼镜还配备有前置摄像头,拥有5P镜头和1600万像素,可以带来第一视角画面,同时其还支持息屏拍照,只需滑动镜角就可以更好的捕捉那些稍纵即逝的画面。新AR眼镜还配备有光

马自达公司最近透露了备受期待的计划,据官方消息,全新的马自达6将于2024年上半年正式发布,并计划在同年年底上市销售。这款全新的马自达6将采用全新的后驱平台,为马自达的车型带来全新的驾驶动力体验马自达的欧洲开发与工程负责人JoachimKunz在最近的一次采访中表示,虽然他们正在开发新平台,但这一平台不会被用于生产全新一代的马自达5轿车,也不会用于其他大型运动轿车的制造。这一决策似乎反映了市场需求的变化,因为现今的消费者更加偏好SUV车型,而不是传统的轿车。Kunz进一步指出,尽管六缸发动机和后

今日头条怎么发布自己的作品?今日头条中是可以发布喜爱的文章作品的,但是多数的小伙伴不知道今日头条如何的发布作品,接下来就是小编为用户带来的发布自己的作品方法图文教程,感兴趣的用户快来一起看看吧!今日头条怎么发布自己的作品1、首先打开今日头条APP,进入到主页面点击右下角【我的】专区;2、然后在我的专区,滑动选择【创作中心】服务功能;3、之后在创作中心的界面,点击右上角的【发布】;4、接着下方展开多功能栏,选择你需要发布作品的方式;5、最后编辑文章点击右上角的【发布】即可发布自己的作品。

5月22日消息,今天的中国日期。在浙江嘉善举办的祥符创新论坛转化医学产业高峰论坛取得了圆满成功。据ITBEAR科技资讯了解,该论坛旨在围绕精准医学及生命科学前沿热点问题展开讨论,并分享最新的诊断技术、基因测序等前沿领域的进展,以推动医学产业的健康发展。与会专家汇聚一堂,分享实践经验和创新思路。论坛期间,还举行了重磅发布和联盟签约仪式。上海人工智能研究院、祥符实验室以及转化医学国家科学中心(上海)联合发布了全国首个DNA存储领域预训练大模型——"ChatDNA"。该模型的发布将

50系显卡预计将会在2024年年底进行发布,从第四季开始将会陆续发布,不过50系显卡笔记本预计要等待27年才会进行发售。RTX5090什么时候发布:答:2024年年底之前1、自2024年第四季开始,NVIDIA首推50系列显卡,预计2024年内正式发布,5090与5080预计秋季冬季上市,70和60则定在2025年公布。同期,NVIDIA自产产品预定2023年面世。值得注意的是,50系列笔记型电脑推广需待时日,或需等到2027年前后方能问世。2、目前,初步预计RTX50系列显卡至少要到2024年

如何在uni-app中实现小程序开发和发布随着移动互联网的发展,小程序成为了移动应用开发的一个重要方向。而uni-app作为一个跨平台的开发框架,可以同时支持多个小程序平台的开发,如微信、支付宝、百度等。下面将详细介绍如何使用uni-app开发和发布小程序,并提供一些具体的代码示例。一、小程序开发前准备在开始使用uni-app开发小程序之前,需要做一些准备工

Maven项目打包步骤简介:一步步实现项目的打包和发布,需要具体代码示例随着软件开发行业的不断发展,Maven作为一个优秀的项目管理工具在项目构建中扮演着重要的角色。Maven不仅可以帮助我们管理项目依赖,还可以通过一系列指令实现项目的打包和发布。本文将针对Maven项目打包的步骤做一简要介绍,并提供具体的代码示例,帮助读者更加深入了解和掌握Maven项目打


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
