Home > database >  Laravel-9 named route, similar to another, fails to match & display content. but SAME content, at DI
Laravel-9 named route, similar to another, fails to match & display content. but SAME content, at DI

Time:07-19

I'm building a new Laravel-9 site.

I've created some test content

For the following routes,

cat routes/web.php
    ...
    Route::prefix('test')->group(function () {
        Route::get(
            '/A',
            function () {
                return View::make('templates.pages.testA.testA');
            }
        );

        Route::get(
            '/',
            function () {
                return View::make('templates.pages.test.test');
            }
        );
    });

    Route::get(
        '/othertest/A',
        function () {
            return View::make('templates.pages.testA.testA');
        }
    );
    ...

at URL

https:/mysite.loc/test/

i see the test content.

at URL

https:/mysite.loc/othertest/A

i also see the test content.

BUT, at URL

https:/mysite.loc/test/A

, which should display the same content as @ the '/othertest/A', I get just a "404 | not found" error.

IIUC, route matches are parsed from top to bottom, so I'd expect "/test/A" to match and not get stomped on by "/test".

What do I need to change in that config for "/test/A" to match/display correctly?

EDIT1:

mv'ing the errant route out of the prefixed group,

cat routes/web.php
    ...
    Route::prefix('test')->group(function () {
-       Route::get(
-           '/A',
-           function () {
-               return View::make('templates.pages.testA.testA');
-           }
-       );

        Route::get(
            '/',
            function () {
                return View::make('templates.pages.test.test');
            }
        );
    });
    
    Route::get(
        '/test/A',
        function () {
            return View::make('templates.pages.testA.testA');
        }
    );

    Route::get(
        '/othertest/A',
        function () {
            return View::make('templates.pages.testA.testA');
        }
    );
    ...

makes no difference -- "/test/A" fails to match/display the same path/content that "/othertest/A" successfully does

EDIT2:

php artisan route:list | egrep -i "test|error"
  GET|HEAD  othertest/A ......................................................................................................................................................................................................
  GET|HEAD  test .............................................................................................................................................................................................................
  GET|HEAD  test/A ...........................................................................................................................................................................................................

CodePudding user response:

You can try clearing route cache

$ php artisan route:clear

CodePudding user response:

Did you clear the route cache after writing new routes? the most probable solution to your problem is:

php artisan cache:clear

php artisan route:cache

Update

Seems like its the problem with syntax, try it this way:

Route::group(['prefix' => 'test'], function () {
    Route::get('/A', function ()    {
        return View::make('templates.pages.testA.testA');
    });
    Route::get('/', function ()    {
        return View::make('templates.pages.test.test');
    });
});

Since the group does exactly the same as prefix with added options passed as array. I believe this should work.

  • Related