I'm learning Laravel and I can't solve one thing. 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. I try modify this: //public const HOME = '/home'; public const HOME = "/profile/{user_id}"; but doesn't working I get this URL http://127.0.0.1:8000/profile/{user_id}
Laravel Version 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}");
}
}
CodePudding user response:
You can override the LoginResponse class.
You can find the LoginResponse file in vendor/laravel/fortify/src/Http/Responses directory. Then create a new php file in your app/Http/Responses directory (if it does not exists, create the directory) named "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;
.
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.
$this->app->singleton(\Laravel\Fortify\Contracts\LoginResponseContract::class, \App\Http\Responses\LoginResponse::class);
Now it's all over.
CodePudding user response:
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.