shankarnewton's avatar

Laravel 7 404 Error with Route with more than 2 Levels

I'm facing a wired issue with Laravel where routes with multiple parameters (both mandatory/optional) aren't working.

Environment Information:

  • Local: Windows, XAMPP, PHP 7.3
  • Production: Ubuntu 18.04, PHP 7.4

Initially, I suspected issue with .htaccess file but that seems not to be an issue. This works perfectly on my Local, but for some reason, that doesn't work on Ubuntu Server.

The following code works perfectly.

Route::any('route/me/','Tst@routeme');

However, any of the following doesn't work:

Route::any('route/me/here/','Tst@routeme');
Route::any('route/me/here/{id?}','Tst@routeme');
Route::any('route/me/here/and/here','Tst@routeme');

Basically any number of parameters added after Level 2 (route/me) doesn't work at all and throws 404 | Not found Error. Any suggestions where I can look up to fix this out, please?

Update:

I posted 3 examples of routes as an example that I tried "individually" but none of them worked.

My .htaccess file is :

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
0 likes
12 replies
Snapey's avatar

if you have routes like

Route::any('route/me/{id}','Tst@routeme');
Route::any('route/me/here','Tst@here');

then the here route will never be reached because the 1st route is matched first. The id is passed as 'here' which results in a 404 when you try and resolve this as a model id in the controller.

In this case, you would switch the order of these routes in the file.

shankarnewton's avatar

@michaloravec @automica , thank you. Sorry if the question was confusing, I only placed 3 such combinations for sake of example, however in real time I am going to use only the one with ID.

Further, for sake of testing I tried removing all other routes. the only route that I have at the moment is

Route::any('route/me/here/{id?}','Tst@routeme');

Routes in my web.php :

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Log;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('route/me/here/{id?}','Tst@routeme');

But that didn't work. Anything I'm doing wrong?

shankarnewton's avatar

Sorry for the confusion, I just posted them constitutively for sake of example, however, I tried each of them individually.

For sake of testing, I removed all the contents of web.php and just placed

Route::any('route/me/{id}','Tst@routeme');

But that didn't work as well.

automica's avatar

@shankarnewton what url are you hitting which 'didn't work'?

if you run

php artisan route:list

does your route appear in that list?

shankarnewton's avatar

Couldn't share the original URL due to company policy restrictions. Kindly pardon me! Am trying http://example.com/route/me/22

I will try a quick UAT setup and would share the URL. I tried

php artisan route:list

For some reason that's not appearing here ;(

priyalaks's avatar
Level 1

Try clearing your

Run

php artisan route:cache 

and then

php artisan route:clear
1 like

Please or to participate in this conversation.