Laravel 在用户个人资料页面登录后重定向
Laravel redirect after Login in user's profile page
我正在学习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/%7Buser_id%7D
Laravel 版本 8.83.5
Routeserviceprovider.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());
});
}
}
Web.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}");
}
}
您可以覆盖 LoginResponse class。
您可以在 vendor/laravel/fortify/src/Http/Responses 目录中找到 LoginResponse 文件。然后在您的 app/Http/Responses 目录中创建一个新的 php 文件(如果它不存在,请创建该目录)命名为“LoginResponse.php”。
然后您需要复制并粘贴您在 vendor 目录中找到的 LoginResponse class 中的代码,并将命名空间行更改为 namespace App\Http\Responses;
.
然后根据需要编辑重定向行。在这种情况下,您需要像下面的代码一样进行编辑。
: redirect()->route('your_profile_route_name', auth()->user()->id);
那么需要在app/Providers/FortifyServiceProvider.
函数里面的boot()
函数末尾添加如下代码
$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponseContract::class, \App\Http\Responses\LoginResponse::class);
现在一切都结束了。
我想你要找的是
App\Http\Controllers\Auth\LoginController.php
登录后从这里进行重定向。如果你进入认证功能。你可以在这里做你的逻辑,比如知道用户是谁并获取他们的 ID 和 return 重定向到他们的个人资料页面。
我正在学习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/%7Buser_id%7D
Laravel 版本 8.83.5
Routeserviceprovider.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());
});
}
}
Web.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}");
}
}
您可以覆盖 LoginResponse class。
您可以在 vendor/laravel/fortify/src/Http/Responses 目录中找到 LoginResponse 文件。然后在您的 app/Http/Responses 目录中创建一个新的 php 文件(如果它不存在,请创建该目录)命名为“LoginResponse.php”。
然后您需要复制并粘贴您在 vendor 目录中找到的 LoginResponse class 中的代码,并将命名空间行更改为 namespace App\Http\Responses;
.
然后根据需要编辑重定向行。在这种情况下,您需要像下面的代码一样进行编辑。
: redirect()->route('your_profile_route_name', auth()->user()->id);
那么需要在app/Providers/FortifyServiceProvider.
函数里面的boot()
函数末尾添加如下代码
$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponseContract::class, \App\Http\Responses\LoginResponse::class);
现在一切都结束了。
我想你要找的是
App\Http\Controllers\Auth\LoginController.php
登录后从这里进行重定向。如果你进入认证功能。你可以在这里做你的逻辑,比如知道用户是谁并获取他们的 ID 和 return 重定向到他们的个人资料页面。