3
4

More than 3 years have passed since last update.

(Laravel6.x)laravel/ui で 5.8 以前の make:auth のscaffolding をする

Last updated at Posted at 2019-12-15

Laravel6.x から make:auth コマンドが消え、react と vue のscaffolding に変わった??

php artisan make:auth

  Command "make:auth" is not defined.

以前から laravel/ui に認証のscaffolding が移行されたから、という情報は認識していたのですが、最近 「laravel/ui の make:auth(ui:auth) では npm が必要で vue、react が defaultになったらしい。」 というのを聞いて公式を調べたところ
image.png

vue が前提となっているように見えるので、今のプロジェクトではSPA用のJSは使っていないため bladeのscaffoldings がこのまま廃止されてしまったら、使いずらくなるなと思ったのですが、調べると、laravel/uiでも 5.8 以前の make:auth のscaffolding をすることができました

0. 準備

laravel new make-auth-demo

1. laravel/ui を取得

composer require laravel/ui --dev

2. ui:auth --views を実行

php artisan ui:auth --views

image.png

image.png

image.png

ui/src/AuthCommand.php にがっつり記載がありました。

ui/src/AuthCommand.php
<?php
namespace Laravel\Ui;
use InvalidArgumentException;
use Illuminate\Console\Command;
class AuthCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ui:auth
                    { type=bootstrap : The preset type (bootstrap) }
                    {--views : Only scaffold the authentication views}
                    {--force : Overwrite existing views by default}';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Scaffold basic login and registration views and routes';
    /**
     * The views that need to be exported.
     *
     * @var array
     */
    protected $views = [
        'auth/login.stub' => 'auth/login.blade.php',
        'auth/passwords/confirm.stub' => 'auth/passwords/confirm.blade.php',
        'auth/passwords/email.stub' => 'auth/passwords/email.blade.php',
        'auth/passwords/reset.stub' => 'auth/passwords/reset.blade.php',
        'auth/register.stub' => 'auth/register.blade.php',
        'auth/verify.stub' => 'auth/verify.blade.php',
        'home.stub' => 'home.blade.php',
        'layouts/app.stub' => 'layouts/app.blade.php',
    ];
    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        if (static::hasMacro($this->argument('type'))) {
            return call_user_func(static::$macros[$this->argument('type')], $this);
        }
        if (! in_array($this->argument('type'), ['bootstrap'])) {
            throw new InvalidArgumentException('Invalid preset.');
        }
        $this->ensureDirectoriesExist();
        $this->exportViews();
        if (! $this->option('views')) {
            $this->exportBackend();
        }
        $this->info('Authentication scaffolding generated successfully.');
    }
    /**
     * Create the directories for the files.
     *
     * @return void
     */
    protected function ensureDirectoriesExist()
    {
        if (! is_dir($directory = $this->getViewPath('layouts'))) {
            mkdir($directory, 0755, true);
        }
        if (! is_dir($directory = $this->getViewPath('auth/passwords'))) {
            mkdir($directory, 0755, true);
        }
    }
    /**
     * Export the authentication views.
     *
     * @return void
     */
    protected function exportViews()
    {
        foreach ($this->views as $key => $value) {
            if (file_exists($view = $this->getViewPath($value)) && ! $this->option('force')) {
                if (! $this->confirm("The [{$value}] view already exists. Do you want to replace it?")) {
                    continue;
                }
            }
            copy(
                __DIR__.'/Auth/'.$this->argument('type').'-stubs/'.$key,
                $view
            );
        }
    }
    /**
     * Export the authentication backend.
     *
     * @return void
     */
    protected function exportBackend()
    {
        file_put_contents(
            app_path('Http/Controllers/HomeController.php'),
            $this->compileControllerStub()
        );
        file_put_contents(
            base_path('routes/web.php'),
            file_get_contents(__DIR__.'/Auth/stubs/routes.stub'),
            FILE_APPEND
        );
    }
    /**
     * Compiles the "HomeController" stub.
     *
     * @return string
     */
    protected function compileControllerStub()
    {
        return str_replace(
            '{{namespace}}',
            $this->laravel->getNamespace(),
            file_get_contents(__DIR__.'/Auth/stubs/controllers/HomeController.stub')
        );
    }
    /**
     * Get full view path relative to the application's configured view path.
     *
     * @param  string  $path
     * @return string
     */
    protected function getViewPath($path)
    {
        return implode(DIRECTORY_SEPARATOR, [
            config('view.paths')[0] ?? resource_path('views'), $path,
        ]);
    }
}

参考

下記の記事に詳しく書いて頂いておりましたので参考にさせて頂きました。
https://medium.com/@panjeh/laravel-changes-in-php-artisan-ui-auth-php-artisan-make-auth-82fdb8893726

以上です。

3
4
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
3
4