1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Route inside a prefix group gives 404 error [duplicate]

Discussão em 'Outras Linguagens' iniciado por Developer lci-agency, Outubro 10, 2024 às 11:12.

  1. I'm using Laravel 11.27.2 and PHP 8.2 and I have noticed that some routes inside a prefix group gives me a 404 error, but if I try move the route outside the prefix group, the route magically works without any type of errors. Also, if I also move the corrupted route on top the other routes inside the prefix group it works.

    I've tried also the command php artisan route:clear but nothing changed.

    Instead with the command php artisan route:list the routes are being listed correctly (Route list).

    This one doesn't work:

    Route::prefix('giveaway')->name('giveaway.')->group(function () {
    Route::get('/{id}', [GiveawayController::class, 'show'])->name('show');
    Route::post('/store/{id}', [GiveawayController::class, 'store'])->name('store');
    Route::get('/thank-you', [GiveawayController::class, 'thankYou'])->name('thankYou');
    Route::get('/terms-and-conditions/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('termsAndConditions');
    Route::get('/privacy-policy/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('privacyPolicy');
    Route::get('/history', [GiveawayController::class, 'index'])->name('history');
    });


    This will work:

    Route::prefix('giveaway')->name('giveaway.')->group(function () {
    Route::get('/history', [GiveawayController::class, 'index'])->name('history'); //Changed position
    Route::get('/{id}', [GiveawayController::class, 'show'])->name('show');
    Route::post('/store/{id}', [GiveawayController::class, 'store'])->name('store');
    Route::get('/thank-you', [GiveawayController::class, 'thankYou'])->name('thankYou');
    Route::get('/terms-and-conditions/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('termsAndConditions');
    Route::get('/privacy-policy/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('privacyPolicy');
    });


    This will also work:

    Route::prefix('giveaway')->name('giveaway.')->group(function () {
    Route::get('/{id}', [GiveawayController::class, 'show'])->name('show');
    Route::post('/store/{id}', [GiveawayController::class, 'store'])->name('store');
    Route::get('/thank-you', [GiveawayController::class, 'thankYou'])->name('thankYou');
    Route::get('/terms-and-conditions/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('termsAndConditions');
    Route::get('/privacy-policy/{id}', [GiveawayController::class, 'privacyAndTerms'])->name('privacyPolicy');
    });

    Route::get('/history', [GiveawayController::class, 'index'])->name('history'); //Outside the route group


    The corrupted routes are /history and /thank-you.

    I would like to figure out why it's happening such a strange thing.

    If helps I'm using Apache as HTTP Server and this is my .htaccess:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ /public/$1 [L,QSA]

    # php -- BEGIN cPanel-generated handler, do not edit
    # Set the “ea-php82” package as the default “PHP” programming language.
    <IfModule mime_module>
    AddHandler application/x-httpd-ea-php82 .php .php8 .phtml
    </IfModule>
    # php -- END cPanel-generated handler, do not edit


    I solved the puzzle, the problem was the route '/{id}' where there was no type of constraint and consequently, being the first route in the group, it was chosen first and did not check if other routes existed.

    SOLUTION:


    1. Move the route to last in that prefix;

    2. Add a constraint with regex expressions on the route for the parmeter with ->where();

    Continue reading...

Compartilhe esta Página