【Laravel】artisanのmake:componentコマンドを解説

Laravel

Laravel 10.xのartisanの「make:component」コマンドを解説します。

Description

新しいビューコンポーネントクラスを作成

Usage

php artisan make:component [-f|--force] [--inline] [--view] [--] <name>

Arguments

引数必須説明
name必須コンポーネント名

Options

オプション省略形必須説明
force--コンポーネントが既に存在する場合でもクラスを作成する
inline--インラインビューをレンダリングするコンポーネントを作成する
view--ビューだけを持つ匿名コンポーネントを作成する

Examples

no options

php artisan make:component Foo
   INFO  Component [app/View/Components/Foo.php] created successfully.  
<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Foo extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return view('components.foo');
    }
}
<div>
    <!-- Simplicity is an acquired taste. - Katharine Gerould -->
</div>

bladeの中身のコメントアウトにinspireが入っているのが良いですね。

inline

php artisan make:component --inline Foo
   INFO  Component [app/View/Components/Foo.php] created successfully.  
<?php

namespace App\View\Components;

use Closure;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Foo extends Component
{
    /**
     * Create a new component instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the view / contents that represent the component.
     */
    public function render(): View|Closure|string
    {
        return <<<'blade'
<div>
    <!-- Simplicity is the ultimate sophistication. - Leonardo da Vinci -->
</div>
blade;
    }
}

view

php artisan make:component --view Foo
   INFO  Component created successfully.  
<div>
    <!-- I begin to speak only when I am certain what I will say is not better left unsaid. - Cato the Younger -->
</div>

Follow me!

コメント

PAGE TOP
タイトルとURLをコピーしました