Skip to content

Commit 7e6a305

Browse files
committed
review authentication & add deployment & add urls
1 parent e2ea6e8 commit 7e6a305

File tree

4 files changed

+234
-6
lines changed

4 files changed

+234
-6
lines changed

authentication.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- [简介](#introduction)
44
- [数据库注意事项](#introduction-database-considerations)
5-
- [认证快速入门](#authentication-quickstart)
5+
- [快速认证](#authentication-quickstart)
66
- [路由](#included-routing)
77
- [视图](#included-views)
88
- [认证](#included-authenticating)
@@ -44,7 +44,7 @@ Laravel 中实现用户认证非常简单。实际上,几乎所有东西都已
4444
此外,你要验证的用户(或等效的)表要包含一个可空的、长度为 100 的字符串 `remember_token`。这个字段将用于存储当用户登录应用并勾选「记住我」时的令牌。
4545

4646
<a name="authentication-quickstart"></a>
47-
## 认证快速入门
47+
## 快速认证
4848

4949
Laravel 自带几个预构建的认证控制器,它们被放置在 `App\Http\Controllers\Auth` 命名空间内。`RegisterController` 处理新用户注册,`LoginController` 处理用户认证,`ForgotPasswordController` 处理用于重置密码的邮件链接,而 `ResetPasswordController` 包含重置密码的逻辑。这些控制器都使用 trait 来引入所必要的方法。对于大多数应用而言,你根本不需要修改这些控制器。
5050

@@ -94,7 +94,7 @@ Laravel 默认使用 `email` 字段来认证。如果你想用其他字段认证
9494

9595
#### 自定义看守器
9696

97-
你还可以自定义用于认证和注册用户的「看守器」。要实现这一功能,需要在 `LoginController``RegisterController``ResetPasswordController` 中定义 `guard` 方法。该方法需要返回一个 guard 实例
97+
你还可以自定义用于认证和注册用户的「看守器」。要实现这一功能,需要在 `LoginController``RegisterController``ResetPasswordController` 中定义 `guard` 方法。该方法需要返回一个看守器实例
9898

9999
use Illuminate\Support\Facades\Auth;
100100

deployment.md

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# 部署
2+
3+
- [简介](#introduction)
4+
- [服务器配置](#server-configuration)
5+
- [Nginx](#nginx)
6+
- [优化](#optimization)
7+
- [优化自动加载](#autoloader-optimization)
8+
- [优化配置加载](#optimizing-configuration-loading)
9+
- [优化路由加载](#optimizing-route-loading)
10+
- [Forge 部署](#deploying-with-forge)
11+
12+
<a name="introduction"></a>
13+
## 简介
14+
15+
当你准备好将 Laravel 应用部署到生产环境时,你可以执行一些操作来确保应用程序尽可能高效地运行。在本文档中介绍一些能确保 Laravel 应用被正确部署。
16+
17+
<a name="server-configuration"></a>
18+
## 服务器配置
19+
20+
<a name="nginx"></a>
21+
### Nginx
22+
23+
如果你将应用程序部署到运行 Nginx 的服务器,可以使用下面的内容来配置 Web 服务器。这个文件可能需要根据你的服务器配置进行自定义。你可以考虑使用 [Laravel Forge](https://forge.laravel.com) 等服务协助管理你的服务器:
24+
25+
server {
26+
listen 80;
27+
server_name example.com;
28+
root /example.com/public;
29+
30+
add_header X-Frame-Options "SAMEORIGIN";
31+
add_header X-XSS-Protection "1; mode=block";
32+
add_header X-Content-Type-Options "nosniff";
33+
34+
index index.html index.htm index.php;
35+
36+
charset utf-8;
37+
38+
location / {
39+
try_files $uri $uri/ /index.php?$query_string;
40+
}
41+
42+
location = /favicon.ico { access_log off; log_not_found off; }
43+
location = /robots.txt { access_log off; log_not_found off; }
44+
45+
error_page 404 /index.php;
46+
47+
location ~ \.php$ {
48+
fastcgi_split_path_info ^(.+\.php)(/.+)$;
49+
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
50+
fastcgi_index index.php;
51+
include fastcgi_params;
52+
}
53+
54+
location ~ /\.(?!well-known).* {
55+
deny all;
56+
}
57+
}
58+
59+
<a name="optimization"></a>
60+
## 优化
61+
62+
<a name="autoloader-optimization"></a>
63+
### 优化自动加载
64+
65+
部署项目到生产环境时,请确保你优化了 Composer 类的自动加载映射,以便 Composer 可以快速找到正确文件为给定类加载:
66+
67+
composer install --optimize-autoloader
68+
69+
> {tip} 除了优化自动加载之外,还应该确保项目的源代码管理库中包含了 `composer.lock` 文件。因为当 `composer.lock` 文件存在时,项目的依赖项可以被更快地安装。
70+
71+
<a name="optimizing-configuration-loading"></a>
72+
### 优化配置加载
73+
74+
将应用部署到生产环境时,记得在部署过程中运行 Artisan 命令 `config:cache`
75+
76+
php artisan config:cache
77+
这个命令可以将所有 Laravel 的配置文件合并到单个文件中缓存,此举能大大减少框架在加载配置值时必须执行的系统文件的数量。
78+
79+
<a name="optimizing-route-loading"></a>
80+
### 优化路由加载
81+
82+
如果你构建的是具有许多路由的大型应用程序,那你应该在部署过程中运行 Artisan 命令 `route:cache`
83+
84+
php artisan route:cache
85+
这个命令可以将所有路由注册减少为缓存文件中的单个方法调用,以达到当应用程序在注册数百条路由时,提高路由注册的性能。
86+
87+
> {note} 由于此功能使用 PHP 序列化,而 PHP 无法序列化闭包,因此只能缓存应用程序中基于控制器的路由。
88+
89+
<a name="deploying-with-forge"></a>
90+
## Forge 部署
91+
92+
如果你还没有准备好管理自己的服务器配置,或者你的服务器没有配置 Laravel 应用程序所需的各种服务,[Laravel Forge](https://forge.laravel.com) 是一个不错的选择。
93+
94+
Laravel Forge 可以在各种基础设施提供商(如 DigitalOcean、Linode、AWS 等)上创建服务器。此外,Forge 还能安装和管理构建 Laravel 应用程序所需的所有工具,如 Nginx、MySQL、Redis、Memcached、Beanstalk 等。
95+
96+
## 译者署名
97+
| 用户名 | 头像 | 职能 | 签名 |
98+
| --- | --- | --- | --- |
99+
| [@JokerLinly](https://laravel-china.org/users/5350) | <img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/5350_1481857380.jpg"> | 翻译 | Stay Hungry. Stay Foolish. |
100+
101+
---
102+
103+
> {note} 欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创开源社区。
104+
>
105+
> 转载请注明:本文档由 Laravel China 社区 [laravel-china.org](https://laravel-china.org) 组织翻译,详见 [翻译召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)
106+
>
107+
> 文档永久地址: https://d.laravel-china.org

mix.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [PostCSS](#postcss)
1111
- [纯 CSS](#plain-css)
1212
- [URL 处理](#url-processing)
13-
- [文件映射](#css-source-maps)
13+
- [资源映射](#css-source-maps)
1414
- [使用 JavaScript](#working-with-scripts)
1515
- [提取 Vendor](#vendor-extraction)
1616
- [React](#react-support)
@@ -188,9 +188,9 @@ Laravel Mix 自带了一个用来转换 CSS 的强大工具 [PostCSS](http://pos
188188
}
189189

190190
<a name="css-source-maps"></a>
191-
### 文件映射
191+
### 资源映射
192192

193-
默认情况下文件映射是禁用的,可以在 `webpack.mix.js` 文件中调用 `mix.sourceMaps()` 方法来开启它。尽管它会带来一些编译/性能的成本,但在使用编译资源时,可以为使用浏览器的开发人员工具提供额外的调试信息:
193+
默认情况下资源映射是禁用的,可以在 `webpack.mix.js` 文件中调用 `mix.sourceMaps()` 方法来开启它。尽管它会带来一些编译/性能的成本,但在使用编译资源时,可以为使用浏览器的开发人员工具提供额外的调试信息:
194194

195195
mix.js('resources/assets/js/app.js', 'public/js')
196196
.sourceMaps();

urls.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# URL 生成
2+
3+
- [简介](#introduction)
4+
- [基础](#the-basics)
5+
- [生成基础 URL](#generating-basic-urls)
6+
- [访问当前 URL](#accessing-the-current-url)
7+
- [命名路由的 URL](#urls-for-named-routes)
8+
- [控制器行为的 URL](#urls-for-controller-actions)
9+
- [默认值](#default-values)
10+
11+
<a name="introduction"></a>
12+
## 简介
13+
14+
Laravel 提供了几个辅助函数来为应用程序生成 URL。主要用于在模板和 API 响应中构建 URL 或者在应用程序的其他部分生成重定向响应。
15+
16+
<a name="the-basics"></a>
17+
## 基础
18+
19+
<a name="generating-basic-urls"></a>
20+
### 生成基础 URL
21+
22+
辅助函数 `url` 可以用于应用的任何一个 URL。生成的 URL 将自动使用当前请求中的方案( HTTP 或 HTTPS )和主机:
23+
24+
$post = App\Post::find(1);
25+
26+
echo url("/posts/{$post->id}");
27+
28+
// http://example.com/posts/1
29+
30+
<a name="accessing-the-current-url"></a>
31+
### 访问当前 URL
32+
33+
如果没有给辅助函数 `url` 提供路径,则会返回一个 `Illuminate\Routing\UrlGenerator` 实例,来允许你访问有关当前 URL 的信息:
34+
35+
// 获取没有查询字符串的当前的 URL ...
36+
echo url()->current();
37+
38+
// 获取包含查询字符串的当前的 URL ...
39+
echo url()->full();
40+
41+
// 获取上一个请求的完整的 URL...
42+
echo url()->previous();
43+
44+
上面的这些方法都可以通过 `URL` [facade](/docs/{{version}}/facades) 访问:
45+
46+
use Illuminate\Support\Facades\URL;
47+
48+
echo URL::current();
49+
50+
<a name="urls-for-named-routes"></a>
51+
## 命名路由的 URL
52+
53+
辅助函数 `route` 可以用于为指定路由生成 URL。命名路由生成的 URL 不与路由上定义的 URL 相耦合。因此,就算路由的 URL 有任何更改,都不需要对 `route` 函数调用进行任何更改。例如,假设你的应用程序包含以下路由:
54+
55+
Route::get('/post/{post}', function () {
56+
//
57+
})->name('post.show');
58+
59+
要生成此路由的 URL,可以像这样使用辅助函数 `route`
60+
61+
echo route('post.show', ['post' => 1]);
62+
63+
// http://example.com/post/1
64+
[Eloquent 模型](/docs/{{version}}/eloquent) 作为参数值传给 `route` 方法,它会自动提取模型的主键来生成 URL。
65+
66+
echo route('post.show', ['post' => $post]);
67+
68+
<a name="urls-for-controller-actions"></a>
69+
## 控制器行为的 URL
70+
71+
`action` 功能可以为给定的控制器行为生成 URL。这个功能不需要你传递控制器的完整命名空间,但你需要传递相对于命名空间 `App\Http\Controllers` 的控制器类名:
72+
73+
$url = action('HomeController@index');
74+
如果控制器方法需要路由参数,那就将它们作为第二个参数传递给 `action` 函数:
75+
76+
$url = action('UserController@profile', ['id' => 1]);
77+
78+
<a name="default-values"></a>
79+
## 默认值
80+
81+
For some applications, you may wish to specify request-wide default values for certain URL parameters. For example, imagine many of your routes define a `{locale}` parameter:
82+
83+
对于某些应用程序,你可能希望为某些 URL 参数的请求范围指定默认值。例如,假设有些路由定义了 `{locale}` 参数:
84+
85+
Route::get('/{locale}/posts', function () {
86+
//
87+
})->name('post.index');
88+
89+
每次都通过 `locale` 来调用辅助函数 `route` 也是一件很麻烦的事情。因此,使用 `URL::defaults` 方法定义这个参数的默认值,可以让该参数始终存在当前请求中。然后就能从 [路由中间件](/docs/{{version}}/middleware#assigning-middleware-to-routes) 调用此方法来访问当前请求:
90+
91+
<?php
92+
93+
namespace App\Http\Middleware;
94+
95+
use Closure;
96+
use Illuminate\Support\Facades\URL;
97+
98+
class SetDefaultLocaleForUrls
99+
{
100+
public function handle($request, Closure $next)
101+
{
102+
URL::defaults(['locale' => $request->user()->locale]);
103+
104+
return $next($request);
105+
}
106+
}
107+
108+
一旦设置了 `locale` 参数的默认值,就不需要在使用辅助函数 `route` 生成 URL 时传递这个值。
109+
110+
## 译者署名
111+
| 用户名 | 头像 | 职能 | 签名 |
112+
| --- | --- | --- | --- |
113+
| [@JokerLinly](https://laravel-china.org/users/5350) | <img class="avatar-66 rm-style" src="https://dn-phphub.qbox.me/uploads/avatars/5350_1481857380.jpg"> | 翻译 | Stay Hungry. Stay Foolish. |
114+
115+
---
116+
117+
> {note} 欢迎任何形式的转载,但请务必注明出处,尊重他人劳动共创开源社区。
118+
>
119+
> 转载请注明:本文档由 Laravel China 社区 [laravel-china.org](https://laravel-china.org) 组织翻译,详见 [翻译召集帖](https://laravel-china.org/topics/5756/laravel-55-document-translation-call-come-and-join-the-translation)
120+
>
121+
> 文档永久地址: https://d.laravel-china.org

0 commit comments

Comments
 (0)