4
0

More than 3 years have passed since last update.

Laravel7 Bladeファイルのひな形を作成するmakeコマンドを自作する

Last updated at Posted at 2020-04-26

概要

前回の記事Laravel7 makeコマンドのひな形をカスタマイズするでスタブのカスタマイズは行えましたが、makeコマンドを追加したい時の例をご紹介します。

環境

  • Laravel 7.5.1

make:blade コマンド

今回は make:blade コマンドを自作します。
他のmake系コマンドはGeneratorCommandクラスを継承しているのでそれに倣って作成します。

app/Console/Commands/BladeMakeCommand.php ファイルに以下の内容を記述します。

app/Console/Commands/BladeMakeCommand.php
<?php declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Symfony\Component\Console\Input\InputOption;

class BladeMakeCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:blade';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new blade file';

    /**
     * Execute the console command.
     *
     * @return mixed
     * @throws FileNotFoundException
     */
    public function handle()
    {
        if (parent::handle() === false && ! $this->option('force')) {
            return false;
        }

        return true;
    }

    /**
     * Parse the class name and format according to the root namespace.
     *
     * @param  string  $name
     * @return string
     */
    protected function qualifyClass($name): string
    {
        return $name;
    }

    /**
     * Get the destination class path.
     *
     * @param  string  $name
     * @return string
     */
    protected function getPath($name): string
    {
        return $this->laravel->basePath('resources/views') . '/' . str_replace('\\', '/', $name).'.blade.php';
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     * @throws FileNotFoundException
     */
    protected function getStub(): string
    {
        if (file_exists($customPath = $this->laravel->basePath('stubs/blade.stub'))) {
            return $customPath;
        }

        throw new FileNotFoundException('stubs/blade.stub file not found.');
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions(): array
    {
        return [
            ['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists'],
        ];
    }
}

stubs/blade.stub ファイルに以下の内容を記述します。

stubs/blade.stub
@extends('layouts.app')

@section('content')

@endsection

make:blade コマンドの使い方

$ php artisan make:blade hello
resources/views/hello.blade.php
@extends('layouts.app')

@section('content')

@endsection

/ で区切るとディレクトリ切って作成できます。

$ php artisan make:blade foo/bar
resources/views/foo/bar.blade.php
@extends('layouts.app')

@section('content')

@endsection
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0