Michael Rotteveel
Michael Rotteveel

Reputation: 89

Route calls wrong controller method

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?

Upvotes: 2

Views: 446

Answers (1)

Alessandro Filira
Alessandro Filira

Reputation: 164

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

Upvotes: 5

Related Questions