简体   繁体   中英

Laravel redirecting to the wrong route

web.php

 Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');

Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create');

Controller

public function create()
{
    $categories = Category::select('id', 'name')->get();

    return view('clientview.gigs.create', compact('categories'));
}


public function info($id)
{


    $gig = Gigs::join('users', 'users.id', '=', 'gigs.created_by')
        ->join('categories', 'categories.id', '=', 'gigs.category_id')
        ->select('gigs.*', 'categories.name as category_name', 'users.name as user_name', 'users.surname')
        ->where('gigs.id', '=', $id)
        ->orderBy('name', 'ASC')
        ->firstOrFail();

    return view('clientview.gigs.info', compact('gig'));
}

When I try to click this:

<a class="dropdown-item" href="{{ route('clientside.gigs.create') }}">Create Gigs</a>

When I click this I can observe from DebugBar that it directs to route ('clientside.gigs_info') I think "/create" thinks it is an /{ID} but however, I direct to a different route

Michael Mano 的回答,确保你在动态之前写在 web.php 静态路由上。

只需在信息路由之前写创建路由,因为它是动态路由(接受参数)所以总是在静态路由之后写动态路由。

You actually created a dynamic route gigs/{id} so anything that comes after gigs will be called as a parameter of gigs. So to fix this change the order in your web.php like below. So it will search for static route first and then go for dynamic route.

Route::get('/gigs/create', [GigsController::class, 'create'])->name('clientside.gigs.create'); Route::get('/gigs/{id}', [GigsController::class, 'info'])->name('clientside.gigs_info');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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