Home  >  Q&A  >  body text

Laravel 8 - How to redirect /{editable_text} route to /{user} route

I've been trying to create a redirect route to lead me to a user profile. The redirect route should be a string/text from the user database and should redirect to the same user profile page.

For example, let's say my user1 has a column called "editable_link" with a value of "abcd123" and can access the profile through the route "www.mywebsite.com/user1", so when someone visits "www.mywebsite. com/", it should redirect him to "www.mywebsite.com/user1".

I tried many methods but nothing worked for me because I am new to coding. Can someone give me some best solution?

This is the content in my web.php:

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\UserController;
use App\Http\Controllers\VisitController;
use App\Http\Controllers\LinkController;
use Illuminate\Auth\Events\Verified;

Route::get('/', function () {
    return view('welcome');
});
Route::get('/verified', function () {
    return view('verified');
});

Auth::routes(['verify' => true]);

Route::group(['middleware' => 'auth', 'prefix' => 'dashboard', ], function() {

    Route::get('/links', [LinkController::class, 'index']);
    Route::get('/links/new', [LinkController::class, 'create'])->middleware('verified');
    Route::post('/links/new', [LinkController::class, 'store']);
    Route::get('/links/{link}', [LinkController::class, 'edit']);
    Route::post('/links/{link}', [LinkController::class, 'update']);
    Route::delete('/links/{link}', [LinkController::class, 'destroy']);
    Route::get('/qr', [LinkController::class, 'qr']);

    Route::get('/settings', [UserController::class, 'settings']);
    Route::get('/settings/edit', [UserController::class, 'edit']);
    Route::get('/settings/profile', [UserController::class, 'profile']);
    Route::get('/settings/help', [UserController::class, 'help']);
    Route::post('/settings/edit', [UserController::class, 'update']);
    Route::post('/settings/profile', [UserController::class, 'update_avatar']);

});

Route::post('/visit/{link}', [VisitController::class, 'store']);
Route::get('/{user}', [UserController::class, 'show'])->name('show');

This is what I want to create:

Route::get('/qr/{editable_link}', function () {
    return redirect('{user}');
Route::get('/{user}', [UserController::class, 'show'])->name('show');
});

I can post any other code you need, thank you.

P粉986028039P粉986028039154 days ago355

reply all(1)I'll reply

  • P粉041856955

    P粉0418569552024-04-07 11:20:34

    You must first check if a route containing the editable_link value exists in the database. Then you can't do it in the route definition because the database there isn't ready yet.

    Of course, you can choose to check for existence via a place where the database is available (such as a controller or middleware).

    Let there be only this route

    Route::get('/{user}', [UserController::class, 'show'])->name('show');

    Then in the UserController show method you have to create the condition like example

    public function show($user)
    {
        // checks if $user parameter is an editable_link that exist in db
        $userWithEditableLink = User::where('editable_link', $user)->first();
        
        // redirect if above exist to the same route but with, for example, username
        if ($userWithEditableLink) {
            return redirect($userWithEditableLink->username);
        }
        
        // do something as, such as
        // $user = User::where('username', $user)->firstOrFail();
    }

    Alternatively, you can create a middleware that also contains the above conditions.

    reply
    0
  • Cancelreply