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>
Stub
Option | Stub file name |
---|---|
- | view-component.stub |
Source
10.x
framework/src/Illuminate/Foundation/Console/ComponentMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
11.x
framework/src/Illuminate/Foundation/Console/ComponentMakeCommand.php at 11.x · laravel/framework
The Laravel Framework. Contribute to laravel/framework development by creating an account on GitHub.
コメント