Laravel에 지어진 Laravel의 발전기 프레임 워크.
Laravel 5.5 :
composer require exfriend/laravel-recipe엔티티를 생성하려면 기본적으로 템플릿과 실제 데이터의 두 가지가 필요합니다.
레시피는 Laravel의 블레이드를 스터브의 템플릿 엔진으로 사용하므로 기본 사용량은 컨트롤러의 뷰를 반환하는 방법과 매우 유사합니다.
모든 수업을 생성하는 첫 번째 레시피를 작성해 봅시다.
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 ' ) );이제이 레시피를위한 전용 클래스를 만들어 쉽게 만들어 봅시다.
앱/레시피/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 변수를 정의하고 있음을 알 수 있습니다.
두 가지 중요한 일이 여기에서 발생합니다.
먼저, 우리는이 레시피에서 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 ();우리는 여기서 모델을 생성하고 있으며 모델은 우리가 자주 생성하고 싶은 것이기 때문에, 우리가 이미 가지고있는 일반적인 클래스 레시피를 기반으로 전용 모델 레시피를 만드는 것이 합리적입니다. 간단한 모델 레시피를 만들자 :
앱/레시피/modelrecipe.php
곧 올 것입니다.