0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel 6* ui で新規ユーザー登録後のリダイレクト先をデフォルトから変更

Posted at

ド初心者です。認識の違いや問題点等ありましたら、どうぞご連絡ください。。。お願いします

やりたいこと

Laravel uiを使って新規登録したユーザーを任意のURLにリダイレクトしたい

これでいけた!

登録系をつかさどるRegisterControllerの中に、

RouteServiceProviderをuse。

そして、同コントローラの中に、、、

protected $redirectTo = RouteServiceProvider::HOME;

を追加!

<?php
namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Providers\RouteServiceProvider;//追加

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;//追加
//以下省略

RouteServiceProviderをuseして、その中にあるHOMEに定義してあるリダイレクト先へ飛べるようにするわけですね

んじゃあ、今度はそのリダイレクト先を任意の場所に指定してあげればOK

というわけで、

RouteServiceProviderに行って指定したいリダイレクト先を追加してあげよう!

app/Providers/RouteServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    public const HOME = '/';//任意のリダイレクト先を追加

    protected $namespace = 'App\Http\Controllers';
//以下省略

定数HOMEを作ってあげて、そこにリダイレクト先を指定すればOK。
public const HOME = '/';とすることで、今回は「/」というURLを設定しました。

一旦は以上とさせていただきます・・・

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?