如何在 Laravel 中实现自己的 Faker 提供程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38250776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement your own Faker provider in Laravel
提问by carte
I want to create a custom provider for Faker in Laravel (e.g. one for a random building name).
我想在 Laravel 中为 Faker 创建一个自定义提供程序(例如一个用于随机建筑物名称的提供程序)。
Where do I store the custom provider in my application and how do I use it?
我在哪里存储自定义提供程序在我的应用程序中以及如何使用它?
回答by user1669496
You should use php artisan
to generate the custom provider...
您应该使用php artisan
生成自定义提供程序...
On the command line, navigate to the root of your app and type...
在命令行上,导航到应用程序的根目录并键入...
php artisan make:provider FakerServiceProvider
That should generate a new provider in the app/Providers
folder. Here is what my register function looks like going off the example in the faker docs.
这应该在app/Providers
文件夹中生成一个新的提供程序。这是我的 register 函数在 faker 文档中的示例中的样子。
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('Faker', function($app) {
$faker = \Faker\Factory::create();
$newClass = new class($faker) extends \Faker\Provider\Base {
public function title($nbWords = 5)
{
$sentence = $this->generator->sentence($nbWords);
return substr($sentence, 0, strlen($sentence) - 1);
}
};
$faker->addProvider($newClass);
return $faker;
});
}
I'm using an anonymous class here. If you have php < 7, you would likely need to create a new file with your new provider class and pass that in. Make sure you also add this new provider to your providers
array in app/config.php
.
我在这里使用匿名类。如果您对PHP <7,你可能会需要创建一个新的文件与新的供应商类,并把它传递。请确保您还新提供程序添加到您的providers
数组app/config.php
。
Now that it's registered, you can grab your new faker class using the following...
现在它已注册,您可以使用以下命令获取新的 faker 类...
$faker = app('Faker');
echo $faker->title;
Additionally, if you go through the docs at https://laravel.com/docs/5.2/facadesyou should also be able to make a Faker
facade quite easily. All the heavy lifting is done, you'd just have to create the new facade class, have getFacadeAccessor
return 'Faker'
, and add it to your facades
array in app/config.php
.
此外,如果您浏览https://laravel.com/docs/5.2/facades 上的文档,您也应该能够Faker
很容易地制作外观。所有繁重的工作都完成了,您只需要创建新的外观类,getFacadeAccessor
返回'Faker'
,并将其添加到您的facades
数组中app/config.php
。
Then you can simply use it like so...
然后你可以像这样简单地使用它......
echo Faker::title;
回答by user580485
Create your custom provider class and save it under app/Faker/CustomProvider.php. Code:
创建您的自定义提供程序类并将其保存在 app/Faker/CustomProvider.php 下。代码:
namespace App\Faker;
use Faker\Provider\Base;
class CustomProvider extends Base
{
public function customName()
{
return $this->generator->sentence(rand(2, 6));
}
}
Then you need just add your custom provider to faker by addProvider method. Example of laravel's factory with adding custom provider:
然后您只需通过 addProvider 方法将您的自定义提供程序添加到 faker。添加自定义提供程序的 laravel 工厂示例:
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Model::class, function(Faker $faker) {
$faker->addProvider(new App\Faker\CustomProvider($faker));
return [
'name' => $faker->customName,
];
});
回答by Stuart Steedman
I found this worked better, as it did not require my $faker
instance to be instantiated with resolve()
:
我发现这更有效,因为它不需要$faker
使用以下命令实例化我的实例resolve()
:
public function register ()
{
$this->app->bind( Generator::class, function ( $app ) {
$faker = \Faker\Factory::create();
$faker->addProvider( new CustomFakerProvider( $faker ) );
return $faker;
} );
}