Ali
Ali

Reputation: 7483

How do I set dynamic prefixes in Laravel routes

heres the situation - I'm building a site thats region based and I need to set it up so that the first segment of the route is a region i.e. of the type:

www.mysite.com/{region}

Currently I have routes set up like this:

www.mysite.com/businesses

www.mysite.com/businesses/show

I've tried a number of tricks but for some reason I can't get this to work:

www.mysite.com/{region}/businesses

in such a way that I need to filter by the {region} variable and that the {region} variable has to prepend every url in the site, also the {region} variable should also be accessible in the controller. I had a look at the localization options except I don't want to change languages here. I'm looking for implementing something of the following:

www.mysite.com/barcelona/businesses

www.mysite.com/new-york/businesses

I already have a table of all regions and slugs and the required relationships. Just trying to get this to work.

Upvotes: 1

Views: 1740

Answers (1)

user1751672
user1751672

Reputation:

Add the region route on top of all other routes, I have similar feature for a project and that fixed it for me.

Route::get('{region?}/businesses', array(
   'as' => 'regionBusinesses',
   'uses' => 'RegionBusinessesController@getBusinesses'
))->where('region', '.*');

In your Controller

class RegionBusinessesController extends SomeController {

public function getBusinesses($region) {
    return View::make('YOUR_VIEW_NAME')->withBusinesses($this->YOUR_MODEL->FETCH_BUSINESSES_METHOD($region));
}

Upvotes: 2

Related Questions