vozaldi vozaldi - 6 months ago 25
PHP Question

Laravel - Check for another prefix if exists

How can I skip a wildcard Route to check for another Route before return it as 404?

Route::get( '{user}', function( $user ){
// Checking for existing user
if( $user = User::getUser( $user ) )
return view( 'templates.user', [ 'user' => $user ] );

// Should check another prefixes before return 404

return abort( 404 );

Route::get( 'foopage', function(){ return view( 'templates.foo' ); });

From code above, if I access
it's return 404 because no username with 'foopage'.

But it's supposed to show
view( 'templates.foo' )

Answer Source

Move the statement Route::get( 'foopage', function(){ return view( 'templates.foo' ); });

above the user route statement.

Route::get( '{user}', function( $user ){

This is because laravel prioritise the order of routes defined and if the foopage route is defined below the user routes, it will consider foobar to be a user route and try to go down that path.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download