phpmyadmin Laravel 8 -如何将/{editable_text}路由重定向到/{user}路由

xurqigkl  于 2022-11-09  发布在  PHP
关注(0)|答案(2)|浏览(97)

我一直在尝试创建重定向路由,将导致我的用户配置文件。重定向路由应该是字符串/文本从数据库的用户,它应该重定向到同一个用户的配置文件页面。
例如,假设我的user 1具有值为“abcd 123”的名为“editable_link”的列和可通过路由“www.example.com“访问的配置文件www.mywebsite.com/user1,因此当某人访问“www.mywebsite.com/abcd123“时,它应将他重定向到“www.mywebsite.com/user1“。
我已经尝试了很多方法,但没有一种方法对我有效,因为我是新的编码。有人能给予我一些最好的解决方案吗?
这是我在我的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');

这是我想创造的东西:

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

我可以发布任何其他代码你需要的,谢谢。

34gzjxbg

34gzjxbg1#

你必须首先检查数据库中是否存在包含editable_link值的路由。然后,你不能在路由定义中进行检查,因为数据库还没有准备好。
您可以选择通过一个数据库可用的地方来检查是否存在,例如控制器或中间件。
让路线只有这一条

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

然后在UserControllershow方法中,您必须创建条件,例如

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();
}

或者,您也可以创建一个包含上述条件的中间件。

nszi6y05

nszi6y052#

这是我的公共函数代码,我无法在评论中发表。@Chay22

public function show(User $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();

        $tittle = $user->tittle;
        $bio = $user->bio;
        $backgroundColor = $user->background_color;
        $buttonBorderRadius = $user->button_border_radius;
        $buttonTextColor = $user->button_text_color;
        $buttonColor = $user->button_color;
        $textColor = $user->text_color;
        $editableLink = $user->editable_link;

        $user->load('links');

        return view('users.show', [
            'user' => $user,
            'tittle' => $tittle,
            'bio' => $bio,
            'backgroundColor' => $backgroundColor,
            'buttonBorderRadius' => $buttonBorderRadius,
            'buttonTextColor' => $buttonTextColor,
            'buttonColor' => $buttonColor,
            'textColor' => $textColor,
            'editableLink' => $editableLink
        ]);
    }

相关问题