简体   繁体   English

Laravel 在用户个人资料页面登录后重定向

[英]Laravel redirect after Login in user's profile page

I'm learning Laravel and I can't solve one thing.我正在学习Laravel,有一件事情我解决不了。 I would like that the users after the login are directed in their own profile, the local domain of the single profile is composed http://127.0.0.1:8000/profile/user_id .我希望登录后的用户在他们自己的配置文件中定向,单个配置文件的本地域由http://127.0.0.1:8000/profile/user_id组成。 I try modify this: //public const HOME = '/home';我尝试修改这个://public const HOME = '/home'; public const HOME = "/profile/{user_id}"; public const HOME = "/profile/{user_id}"; but doesn't working I get this URL http://127.0.0.1:8000/profile/%7Buser_id%7D但不起作用我得到这个 URL http://127.0.0.1:8000/profile/%7Buser_id%7D

Laravel Version 8.83.5 Laravel 版本 8.83.5

Routeserviceprovider.PHP 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 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 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}");

    }       
}

You can override the LoginResponse class.您可以覆盖 LoginResponse class。

You can find the LoginResponse file in vendor/laravel/fortify/src/Http/Responses directory.您可以在 vendor/laravel/fortify/src/Http/Responses 目录中找到 LoginResponse 文件。 Then create a new php file in your app/Http/Responses directory (if it does not exists, create the directory) named "LoginResponse.php".然后在您的 app/Http/Responses 目录中创建一个新的 php 文件(如果它不存在,请创建该目录),命名为“LoginResponse.php”。

Then you need to copy & paste that the code inside LoginResponse class that you found in vendor directory and change the namespace line as namespace App\Http\Responses;然后,您需要复制并粘贴您在 vendor 目录中找到的 LoginResponse class 中的代码,并将名称空间行更改为namespace App\Http\Responses; . .

Then edit redirect line as you want.然后根据需要编辑重定向行。 In this situation you need to edit like following code.在这种情况下,您需要像下面的代码一样进行编辑。

: redirect()->route('your_profile_route_name', auth()->user()->id);

Then you need to add the following code in the end of boot() function inside the app/Providers/FortifyServiceProvider.然后需要在app/Providers/FortifyServiceProvider里面的boot() function的最后添加如下代码。

$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponseContract::class, \App\Http\Responses\LoginResponse::class);

Now it's all over.现在一切都结束了。

I think what you are looking for is我想你要找的是

App\Http\Controllers\Auth\LoginController.php

From here is where you do the redirecting after log in. If you go into the authenticated function. You can do your logic here like know who the user is and grab their id and return a redirect to their profile page.登录后从这里进行重定向。如果您 go 进入经过身份验证的 function。您可以在此处执行您的逻辑,例如了解用户是谁并获取他们的 ID 并将重定向返回到他们的个人资料页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM