質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

2回答

3711閲覧

Laravel,jetstreamでredirect先を変更したい

koki.0429

総合スコア39

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2021/04/28 23:57

前提・実現したいこと

Laravelを用いて、ECサイト作成中です!
そこで、ログイン機能を実装すべく、jetstreamをDLし、ユーザー情報の登録、ログイン機能の実装は完了。
しかし、ユーザー情報登録後・ログイン後に遷移するページの指定がうまくいかず。

助言・アドバイス等あれば、ご教授いただけると幸いです!

該当のソースコード

<?php namespace App\Http\Middleware; use App\Providers\RouteServiceProvider; use Closure; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class RedirectIfAuthenticated { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @param string|null ...$guards * @return mixed */ public function handle(Request $request, Closure $next, ...$guards) { $guards = empty($guards) ? [null] : $guards; foreach ($guards as $guard) { if (Auth::guard($guard)->check()) { return redirect(RouteServiceProvider::HOME); } } return $next($request); } }
<?php namespace App\Providers; use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Http\Request; use Illuminate\Support\Facades\RateLimiter; use Illuminate\Support\Facades\Route; class RouteServiceProvider extends ServiceProvider { /** * The path to the "home" route for your application. * * This is used by Laravel authentication to redirect users after login. * * @var string */ public const HOME = 'product'; /** * The controller namespace for the application. * * When present, controller route declarations will automatically be prefixed with this namespace. * * @var string|null */ // protected $namespace = 'App\Http\Controllers'; /** * Define your route model bindings, pattern filters, etc. * * @return void */ public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); }); } /** * Configure the rate limiters for the application. * * @return void */ protected function configureRateLimiting() { RateLimiter::for('api', function (Request $request) { return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip()); }); } }

ちなみに、routeは下記のようになっており、情報登録後・ログイン後に遷移したいページは、product.indexです!

<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('contact', 'App\Http\Controllers\ContactController@index')->name('contact.index'); Route::post('contact/confirm', 'App\Http\Controllers\ContactController@confirm')->name('contact.confirm'); Route::post('contact/thanks', 'App\Http\Controllers\ContactController@send')->name('contact.send'); Route::get('product/about', 'App\Http\Controllers\ProductController@about')->name('product.about'); Route::resource('product', 'App\Http\Controllers\ProductController'); Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () { return view('dashboard'); })->name('dashboard');

試したこと

調べてみたところ、定数HOMEの値を変更すればそのファイルに遷移できるとのことであるため、記載している/productなどに変更するなど試してみましたが、だめでした。

補足情報(FW/ツールのバージョンなど)

php 7.3.24
Laravel8.35.1
jetstream

他に必要な情報あれば追記しますので、お手数ですがご教授よろしくお願い致します!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

リダイレクト先はconfig/fortify.php

'home' => RouteServiceProvider::HOME,

https://github.com/laravel/fortify/blob/b45807430194c73aaa9b2f26cb76038cc747466f/stubs/fortify.php#L64

使われてる箇所はこの辺。

php

1redirect()->intended(config('fortify.home'));

https://github.com/laravel/fortify/blob/b45807430194c73aaa9b2f26cb76038cc747466f/src/Http/Responses/LoginResponse.php

結局RouteServiceProvider::HOMEだけどHOMEだけ変えて動かないなら質問に書いてない部分に原因がある。

Jetstreamは「最初からJetstream付きでプロジェクト作成」もしくは「新規作成直後にJetstreamをインストール」しないと正しくインストールできない可能性がある。
別のプロジェクトを作って試した方がいい。

投稿2021/04/29 01:10

kawax

総合スコア10377

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

koki.0429

2021/04/29 03:17

回答ありがとうございます! 新規プロジェクト作成時にインストールする必要があるのは知らなかったので、非常に参考になりました! ありがとうございます! もう少し、他のことも試してみて、それでも実装できないようでしたら、もう一度初めからやり直したいと思います! ありがとうございました!
guest

0

自己解決

下記のredirect先を変更することで、ログイン、ログアウト後にトップページに遷移することができました!
共有しておきます!

//ログイン後の遷移先 <?php namespace Laravel\Fortify\Http\Responses; use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract; class LoginResponse implements LoginResponseContract { /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function toResponse($request) { return $request->wantsJson() ? response()->json(['two_factor' => false]) // : redirect()->intended(config('fortify.home')); : redirect('/product'); } } //ログアウト後 <?php namespace Laravel\Fortify\Http\Responses; use Illuminate\Http\JsonResponse; use Illuminate\Http\Response; use Laravel\Fortify\Contracts\LogoutResponse as LogoutResponseContract; class LogoutResponse implements LogoutResponseContract { /** * Create an HTTP response that represents the object. * * @param \Illuminate\Http\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function toResponse($request) { return $request->wantsJson() ? new JsonResponse('', 204) : redirect('/product'); } }

投稿2021/04/30 22:09

koki.0429

総合スコア39

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問