Support the ongoing development of Laravel.io →
posted 9 years ago
Requests
Last updated 2 years ago.
0

You can register only one route. You need an if ;)

0

Mkuhneu said:

You can register only one route. You need an if ;)

You mean in this case, the practice is to keep one route for listing both and keeping a if condition in controller function ?

0

All routes should have something that sets them apart from other routes, the two url's that you have provided are as far as the computer is concerned: identical. As Mkuhneu said, for the behavior that you want you need a conditional statement at the routing level that determines if the input is a city or a state.

Route::get("/{input}-items.html", function( $input ){
    $app = app();
    $controller = $app->make("CityController");
    $longArrayOfStateNames = [ ... ];

    // Search by state names because the array will be much, much smaller
    if ( in_array($input, $longArrayOfStateNames) )
    {
        return $controller->callAction(
            $app, $app['router'], 'searchState', $parameters = array()
        );
    }else{
        return $controller->callAction(
            $app, $app['router'], 'searchCity', $parameters = array()
        );
    }
});

Note: the above code is an example of how you might do this but remains untested as I just wrote it here as an example and have not tried it to see if it works 100% I do however hope it helps and please feel free to ask for more clarification if you need it.

Also the conditional doesn't have to be at the routing level, you could have it within your controller or better still within some middle-ware which passes the record through to the controller method.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.