Tales
Tales

Reputation: 1923

IgnoreRoute and specific controllers for page requests in Laravel

Let's say I have a PagesController for my frontend pages and when someone requests a slug, the general route is:

Route::get( '{slug}', 'PagesController@show' );

But I also have two specific routes in my frontend which are handled by different controllers:

Route::get( '/login', 'UsersController@index' );

and

Route::get( '/register', 'RegistrationController@create' );

So, I want to create exceptions for both static pages when they get requested. I made some research and someone with a similar problem was suggested to go ahead and create two different middlewares because he had Pages and Users, but that's not my case, I have just two exceptions to the rule here and I want those two special cases treated different. Right now I have the show method taking care of the special cases with if/else statements (because for some reason the specific routes are not getting called, even if I switch the {slug} route to the bottom), but I want the best possible solution. My code looks like this (it will use the $request variable when it is properly implemented)...

    public function show( Request $request ){
        $uri = $request->path();
        if( $uri == 'login' ){
            return view( 'auth.login' );
        }
        elseif( $uri == 'register' ){
            return view( 'registration.register' );
        }
        return view( 'Pages::show' );
    }

Upvotes: 0

Views: 58

Answers (1)

fubar
fubar

Reputation: 17398

What you're suggesting is not the way to do this.

When Laravel handles a request, it will attempt to match the request URI against your application routes in the order they're registered. Therefore, you simply need to include the login and register routes before the page route in your routes/web.php file.

Route::get( '/login', 'UsersController@index' );
Route::get( '/register', 'RegistrationController@create' );
Route::get( '{slug}', 'PagesController@show' );

Upvotes: 1

Related Questions