2
votes

In my Laravel 7 project project I've got two controllers. One for the frontend and one for the backend pages.

The routes are defined as follows:

<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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( '/', 'PageController@index' );
Route::get( '/login', 'Auth\LoginController@showLoginForm' );
Route::get( '/logout', 'Auth\LoginController@logout' );
Route::get( '/register', 'Auth\RegisterController@showRegistrationForm' );
Route::get( '/{slug}', 'PageController@show' );


Auth::routes();

Route::get( '/admin', 'HomeController@index' )->name( 'home' );

However when I try to acces the admin route, it's using the pageController@show method instead of the homeController@index as seen below:

enter image description here

I've tried using groups with prefixes of "admin" and then pages like admin/dashboard use the right controller but still the admin route on itself does not.

I've looked at multiple examples of route files but they don't seem to work for me.

I suspect it's got something to do with the fact that I use dynamic routes? But than again all the other routes work fine so I don't really see the problem here...

How can I fix this?

1
Firstly see /{slug} so its think admin one slug. Move your admin codes before slugxNoJustice
Yes, its an ordering/specificity issue. /{slug} will capture everything there, so you need to put your admin route before it.Kurt Friars
The other comments are right, but for future reference: please post your code directly, not as images.MrEvers
@KurtFriars Trying this right now. If that's the case I'm going to feel really sillyMichael Rotteveel
@MichaelRotteveel it 100% will. The routes attempt to match in top to bottom order, when one matches it uses the route. If none match, it will try to fall back to similar routes. If nothing is found, 404.Kurt Friars

1 Answers

4
votes

It's because you defined "/{slug}" before "/admin" and it's match before. If you invert the order, it should works.