Laravelに構築されたLaravelのジェネレーターフレームワーク。
Laravel 5.5で:
composer require exfriend/laravel-recipeエンティティを生成するには、基本的にテンプレートと実際のデータの2つのものが必要です。
レシピでは、Laravel's Bladeをスタブ用のテンプレートエンジンとして使用するため、基本的な使用法は、コントローラーからのビューを返す方法と非常に似ています。
クラスを生成する最初のレシピを書きましょう。
resources/views/recipeフォルダー内の新しいビューを作成します。リソース/ビュー/レシピ/class.blade.php
{!! ' < ' . ' ?php ' ! !}
@unless ( empty ( $namespace ) )
namespace {{ $namespace } } ;
@endunless
@unless ( empty ( $imports ) )
@foreach ( $imports as $import )
import {{ $import } } ;
@endforeach
@endunless
class {{ $class } } {{ isset ( $extends ) ? ' extends ' . $extends : ' ' } } {{ ! empty ( $implements ) ? ' implements ' . collect ( $implements ) -> implode ( ' , ' ) : ' ' } }
{
@unless ( empty ( $traits ) )
use {{ collect ( $traits ) -> implode ( ' , ' ) } } ;
@endunless
@isset ( $content )
{!! $content ! !}
@endisset
}
次に、コードのどこでも実行できます。
$recipe = recipe()->usingView( 'recipes.class' )->with( [
'namespace' => 'App',
'class' => 'User',
'extends' => 'Authenticatable',
'imports' => [
'IlluminateFoundationAuthUser as Authenticatable',
'IlluminateNotificationsNotifiable',
'LaravelPassportHasApiTokens',
],
'traits' => [
'HasApiTokens',
'Notifiable',
],
// 'implements' => [ 'SomeInterface', 'OtherInterface' ],
] );
コンパイルされたコードを取得します:
dd ( $ recipe -> build () )ファイルに保存:
$ recipe -> build ( app_path ( ' User.php ' ) );次に、このレシピの専用クラスを作成して、簡単にしましょう。
App/Recipes/ClassRecipe.php
<?php
namespace App Recipes ;
class ClassRecipe extends Exfriend Recipe Recipe
{
public $ props = [
' class ' => [
' rules ' => ' required ' ,
],
' content ' => [ ' default ' => '' , ],
' imports ' => [ ' default ' => [], ],
];
protected $ view_name = ' recipes.class ' ;
}
ここでは、テンプレート名を強調しており、Vueがコンポーネントで使用するものと多少似た新しい$props変数を定義していることに気付くことができます。
ここで2つの重要なことが起こります:
最初に、このレシピでclassプロパティが必須であることをレシピに伝える検証を追加しました。 Laravelアプリケーションで通常と同じようにルールプロパティを設定できます - それは同じことです。
次に、 contentとimportのデフォルト値を設定します。ユーザーが入力として何も提供しない場合、これらのデフォルトは適用されます。
したがって、結果として得られる使用法は次のようになります。
$ recipe = ( App Recipes ClassRecipe::class )-> with ( [
' namespace ' => ' App ' ,
' class ' => ' User ' ,
' extends ' => ' IlluminateFoundationAuthUser ' ,
] )
-> build ( app_path ( ' User.php ' ) );重要なメモ:
小道具のため、テンプレートに渡された実際のデータは、私たちが渡したものとはわずかに異なります。たとえば、 contentとimportsがあります。時には、テンプレート全体をコンパイルして変換されたデータを取得したい場合があります(ネストされたレシピなど、以下を参照)。コンパイルされたデータのみを取得するには、実行してください。
$ recipe = ( App Recipes ClassRecipe::class )-> with ( [
...
] )
-> buildData ();ここでモデルを生成し、モデルは頻繁に生成したいものなので、すでに持っている一般的なクラスのレシピに基づいて専用のモデルレシピを作成することは理にかなっています。シンプルなモデルレシピを作りましょう:
App/Recipes/ModelRecipe.php
近日公開。