Home > Enterprise >  Laravel 9 Route problem return 404 NOT FOUND
Laravel 9 Route problem return 404 NOT FOUND

Time:05-03

I'm trying to render a view but I always get notfoud 404 I don't know how to solve this question anymore if anyone can help me I would be grateful. any help is welcome. Thanks in advance. Everyone have a great Monday.

enter image description here

CommunityPostController -> code ->

public function creategif(Community $community)
{
    return view('posts.creategif', compact('community'));
}

Web Routes

Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('u/{id}', [App\Http\Controllers\HomeController::class, 'user'])->name('user.view');

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


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

    Route::resource('communities', \App\Http\Controllers\CommunityController::class);
    Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
    Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);
    Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);


    Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});

Views Files Structure

enter image description here

CodePudding user response:

There might be a conflict base on your route order and the image you've posted. As you can see you have resource for your route which in this case this conflict might happen base on route order, and your laravel is actually trying to get a post instead of loading creategif route.

Which in this case every time you try to get access to creategif route, your application is actually trying to load a post base on 'communities.posts' route.

enter image description here

So base on this conflict in your route order, this is working fine and create or any other routes related to 'communities.posts' should work fine, but in other-hand creategif in URL might recognized as a route related to 'communities.posts' resource.

Move your route to top, or in this case just above 'communities.posts' route, don't forget to clear route cache.

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

    Route::get('communities/{community}/posts/creategif', [\App\Http\Controllers\CommunityPostController::class, 'creategif']);
    Route::resource('communities', \App\Http\Controllers\CommunityController::class);
    Route::resource('communities.posts', \App\Http\Controllers\CommunityPostController::class);
    Route::resource('posts.comments', \App\Http\Controllers\PostCommentController::class);


    Route::get('posts/{post_id}/vote/{vote}', [\App\Http\Controllers\CommunityPostController::class, 'vote'])->name('post.vote');
});
  • Related