Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

已基于4.1文档调整更新4.2所有cn下文件 #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions cn/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# 缓存

- [配置](#configuration)
- [缓存用法](#cache-usage)
- [增加 & 减少](#increments-and-decrements)
- [缓存标签](#cache-tags)
- [数据库缓存](#database-cache)

<a name="configuration"></a>
## 配置

Laravel 对不同的缓存机制提供了一套统一的API。缓存配置信息存放于`app/config/cache.php`文件。在该配置文件中,你可以指定整个应用程序所使用的缓存驱动器。Laravel自身支持大多数流行的缓存服务器,例如[Memcached](http://memcached.org)和[Redis](http://redis.io)。

缓存配置文件还包含了其他配置项,文件里都有详细说明,因此,请务必查看这些配置项和其描述信息。默认情况下,Laravel被配置为使用`file`缓存驱动,它将数据序列化,并存放于文件系统中。在大型应用中,强烈建议使用基于内存的缓存系统,例如Memcached或APC。

<a name="cache-usage"></a>
## 缓存用法

#### 将某一数据存入缓存

Cache::put('key', 'value', $minutes);

#### Using Carbon Objects To Set Expire Time

$expiresAt = Carbon::now()->addMinutes(10);

Cache::put('key', 'value', $expiresAt);

#### 当某一数据不在缓存中是才将其保存

Cache::add('key', 'value', $minutes);

如果该项实际上 **已经添加** 到缓存中,那么 `add` 方法将返回 `true` 。否则,此方法将返回 `false`。

#### 检查缓存中是否有某个key对应的数据

if (Cache::has('key'))
{
//
}

#### 从缓存中取得数据

$value = Cache::get('key');

#### 从缓存中取得数据,如果数据不存,则返回指定的默认值

$value = Cache::get('key', 'default');

$value = Cache::get('key', function() { return 'default'; });

#### 将数据永久地存于缓存中

Cache::forever('key', 'value');

有时你可能想从缓存中取得某项数据,但是还希望在数据不存在时存储一项默认值。那就可以通过 `Cache::remember`方法实现:

$value = Cache::remember('users', $minutes, function()
{
return DB::table('users')->get();
});

还可以将`remember`和`forever`方法结合使用:

$value = Cache::rememberForever('users', function()
{
return DB::table('users')->get();
});

注意:所有存在于缓存中的数据都是经过序列化的,因此,你可以存储任何类型的数据。

#### Pulling An Item From The Cache

If you need to retrieve an item from the cache and then delete it, you may use the `pull` method:

$value = Cache::pull('key');

#### 从缓存中删除某项数据

Cache::forget('key');

<a name="increments-and-decrements"></a>
## 增加 & 减少

除了`文件`和`数据库`驱动器,其他驱动器都支持`增加`和`减少`操作:

#### 让某个值增加

Cache::increment('key');

Cache::increment('key', $amount);

#### 让某个值减少

Cache::decrement('key');

Cache::decrement('key', $amount);

<a name="cache-tags"></a>
## 缓存标签

> **注意:** 当使用 `file` 或者 `database` 缓存驱动时,是不支持缓存标签的。此外,在使用多个缓存标签时它们将存储为 "forever"。使用一个如 `memcached` 的驱动性能将会是最好的,它会自动清除过时的记录。

#### 访问一个标记的缓存

缓存标签允许你在缓存中标记相关的项目,然后刷新指定名称标签的所有缓存。要访问标记的缓存,请使用 `tags` 方法:

你可以通过传递标签名称的有序列表或数组作为参数,来存储一个标记的缓存。

Cache::tags('people', 'authors')->put('John', $john, $minutes);

Cache::tags(array('people', 'artists'))->put('Anne', $anne, $minutes);

你可以在所有缓存存储方法中使用标签,包括 `remember`,`forever`,和 `rememberForever`。你也可以从缓存标签来访问已缓存的项目,以及使用其它缓存方法如 `increment` 和 `decrement`:

#### 从标记的缓存中访问条目

通过与保存时所用相同的标签,作为参数列表来访问标记的缓存。

$anne = Cache::tags('people', 'artists')->get('Anne');

$john = Cache::tags(array('people', 'authors'))->get('John');

你可以通过标签名称(或名称列表)来刷新所有相关的缓存项。例如,下面的语句将移除所有标签中包含 `people` 和 `authors` 的缓存项。因此无论是之前例子中的 "Anne" 还是 "John" 都将从缓存中移除:

Cache::tags('people', 'authors')->flush();

相比之下,下面的语句将移除标签中仅包含 'authors' 的缓存项,因此 "John" 将被移除,但不影响 "Anne" 。

Cache::tags('authors')->flush();

<a name="database-cache"></a>
## 数据库缓存

当使用`数据库`缓存驱动时,你需要设置一个数据表来缓存数据。以下案例是`Schema`表的定义:

Schema::create('cache', function($table)
{
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});

译者:王赛 [(Bootstrap中文网)](http://www.bootcss.com)
139 changes: 139 additions & 0 deletions cn/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# Artisan 开发

- [简介](#introduction)
- [构建命令](#building-a-command)
- [注册命令](#registering-commands)
- [调用其他命令](#calling-other-commands)

<a name="introduction"></a>
## 简介

除了 Artisan 已经提供的命令,你也可以为应用程序构建自己的命令。你可以将自己定制的命令保存在 `app/commands` 目录下,同样,你也可以选择任意的存储目录,只要你的命令能够按照 `composer.json` 中的设置被自动加载即可。

<a name="building-a-command"></a>
## 构建命令

### 生成类

为了创建一个新命令,你可以使用Artisan中的 `command:make` 命令生成一个骨架作为你的起点:

#### 生成一个命令类

php artisan command:make FooCommand

默认情况下,生成的类文件被存放在 `app/commands` 目录下,同时你也可以指定自定义目录和命名空间:

php artisan command:make FooCommand --path=app/classes --namespace=Classes

当创建命令时,可以使用 `--command` 选项来指定终端命令的名称:

php artisan command:make AssignUsers --command=users:assign

### 实现命令

一旦命令被生成,你应当填写这个类的 `name` 和 `description` 属性,这些属性将在展示命令的 `list` 页面显示出来。

当命令运行的时候 `fire` 函数将被调用。你可以在这个函数里实现任何业务逻辑。

### 参数 & 选项

可以通过 `getArguments` 和 `getOptions` 函数为命令定义任何接受的参数和选项。这两个函数都将返回一个数组。

当定义 `arguments`,数组定义值表示如下:

array($name, $mode, $description, $defaultValue)

参数 `mode` 的值可以是 `InputArgument::REQUIRED`, `InputArgument::OPTIONAL` 中的任意一个。

当定义 `options`,数组定义值表示如下:

array($name, $shortcut, $mode, $description, $defaultValue)

对于选项,参数 `mode` 的值可以是`InputOption::VALUE_REQUIRED`, `InputOption::VALUE_OPTIONAL`, `InputOption::VALUE_IS_ARRAY`, `InputOption::VALUE_NONE` 中的一个。

`VALUE_IS_ARRAY` 模式表明,该开关可用于多次调用该命令时:

php artisan foo --option=bar --option=baz

`VALUE_NONE` 选项表明该选项值只能用来作为一个开关:

php artisan foo --option

### 获取输入

当命令执行的时候,你显然需要获取该命令所接收的参数和选项。要做到这一点,你可以使用 `argument` 和 `option` 函数:

#### 获取一个参数的值

$value = $this->argument('name');

#### 获取所有参数

$arguments = $this->argument();

#### 获取一个选项的值

$value = $this->option('name');

获取所有选项

$options = $this->option();

### 输出

你可以使用`info`、`comment`、`question` 和 `error`方法将输出发送到控制台。这些函数中的每一个将根据它们的目的使用合适的 ANSI 颜色。

#### 发送信息到终端

$this->info('Display this on the screen');

#### 发送错误消息到终端

$this->error('Something went wrong!');

### 询问输入

你可以使用 `ask` 和 `confirm` 函数提示用户输入:

#### 询问用户输入

$name = $this->ask('What is your name?');

#### 询问用户输入密码

$password = $this->secret('What is the password?');

#### 询问用户确认

if ($this->confirm('Do you wish to continue? [yes|no]'))
{
//
}

你也可以为 `confirm` 指定一个默认值,应该是 `true` 或者 `false`:

$this->confirm($question, true);

<a name="registering-commands"></a>
## 注册命令

#### 注册一个 Artisan 命令

一旦你的命令完成后,你需要使用 Artisan 进行注册,这样才能够被使用。这通常在 `app/start/artisan.php` 文件中完成。在这个文件中,你可以使用 `Artisan::add` 函数注册命令:

**注册一个 Artisan 命令**

Artisan::add(new CustomCommand);

#### 注册一个在 IoC 容器中的命令

如果你的命令在应用程序的 [IoC 容器](/docs/ioc) 中注册,你可以使用 `Artisan::resolve` 函数使它对 Artisan 可用:

Artisan::resolve('binding.name');

<a name="calling-other-commands"></a>
## 调用其他命令

有时你可能希望在你的命令中调用其他命令,你可以通过使用 `call` 函数实现:

$this->call('command.name', array('argument' => 'foo', '--option' => 'bar'));
3 changes: 3 additions & 0 deletions cn/contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Contribution Guidelines

If you are submitting documentation for the **current stable release**, submit it to the corresponding branch. For example, documentation for Laravel 4.1 would be submitted to the `4.1` branch. Documentation intended for the next release of Laravel should be submitted to the `master` branch.
Loading