我正在學習 Laravel,但我無法解決一件事。我希望登錄后的用戶被定向到他們自己的組態檔中,單個組態檔的本地域由http://127.0.0.1:8000/profile/user_id組成。我嘗試修改這個: //public const HOME = '/home'; public const HOME = "/profile/{user_id}"; 但不起作用我得到這個 URL http://127.0.0.1:8000/profile/{user_id}
Laravel 版本 8.83.5
路由服務提供者.PHP
<?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 = "/profile/{user_id}";
/**
* 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());
});
}
}
網頁.php
<?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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/', 'App\Http\Controllers\ProfilesController@index');
Route::get('/c/create', 'App\Http\Controllers\CoursesController@create');
Route::get('/c/{course}', 'App\Http\Controllers\CoursesController@show');
Route::post('/c', 'App\Http\Controllers\CoursesController@store');
Route::get('/profile/{user}', [App\Http\Controllers\ProfilesController::class, 'index'])->name('profile.show');
Route::get('/profile/{user}/edit', 'App\Http\Controllers\ProfilesController@edit')->name('profile.edit');
Route::patch('/profile/{user}', 'App\Http\Controllers\ProfilesController@update')->name('profile.update');
Profilescontroller.PHP
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Intervention\Image\Facades\Image;
class Profilescontroller extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
// public function invoke()
//{
//$users = auth()->user()->profile()->pluck('profiles.user_id');
//$profiles = Profilescontroller::whereIn('user_id', $users)->get();
//return view("/profile/{$user->id}");
//}
public function index(User $user)
{
$this->authorize('update', $user->profile);
return view('profiles.index', compact('user'));
}
public function edit(User $user)
{
$this->authorize('update', $user->profile);
return view('profiles.edit', compact('user'));
}
public function update(User $user)
{
$this->authorize('update', $user->profile);
$data = request()->validate([
'description' => '',
'image' => '',
]);
if (request('image')) {
$imagePath = request('image')->store('profile', 'public');
$image = Image::make(public_path("storage/{$imagePath}"))->fit(1000, 1000);
$image->save();
$imageArray = ['image' => $imagePath];
}
auth()->user()->profile->update(array_merge(
$data,
$imageArray ?? []
));
return redirect("/profile/{$user->id}");
}
}
uj5u.com熱心網友回復:
我想你正在尋找的是
App\Http\Controllers\Auth\LoginController.php
從這里是您在登錄后進行重定向的地方。如果您進入經過身份驗證的功能。您可以在這里執行您的邏輯,例如知道用戶是誰并獲取他們的 id 并將重定向回傳到他們的個人資料頁面。
uj5u.com熱心網友回復:
您可以覆寫 LoginResponse 類。
您可以在 vendor/laravel/fortify/src/Http/Responses 目錄中找到 LoginResponse 檔案。然后在您的 app/Http/Responses 目錄中創建一個新的 php 檔案(如果不存在,則創建該目錄),命名為“LoginResponse.php”。
然后,您需要復制并粘貼您在供應商目錄中找到的 LoginResponse 類中的代碼,并將命名空間行更改為namespace App\Http\Responses;.
然后根據需要編輯重定向行。在這種情況下,您需要像以下代碼一樣進行編輯。
: redirect()->route('your_profile_route_name', auth()->user()->id);
然后您需要boot()在 app/Providers/FortifyServiceProvider 內的函式末尾添加以下代碼。
$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponseContract::class, \App\Http\Responses\LoginResponse::class);
現在一切都結束了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461490.html
