Answer the question
In order to leave comments, you need to log in
How to make subdomains for cities on laravel (almost succeeded)?
Hello dear programmers!
Question of my day)
Already implemented subdomains for cities on Laravel, but ran into a problem!
I want:
Moscow to be without the website.ru subdomain
Novosibirsk with the nsk.website.ru subdomain
Berdsk with the berdsk.website.ru subdomain I
did this:
DB
Routes
Route::group([
'domain' => '{city_alias}.website.ru',
'namespace' => 'Available',
], function (){
Route::get('/', '[email protected]')->name('index');
});
route('category.one', [$city_one->alias, $category_one->alias])
class IndexController extends Controller
{
public function index($city_alias)
{
$city_one = City::cityAlias($city_alias)->first();
return view('........');
}
}
/**
* Get or set the domain for the route.
*
* @param string|null $domain
* @return $this|string|null
*/
public function domain($domain = null)
{
if (is_null($domain)) {
return $this->getDomain();
}
$parsed = RouteUri::parse($domain);
$this->action['domain'] = $parsed->uri;
$this->bindingFields = array_merge(
$this->bindingFields, $parsed->bindingFields
);
return $this;
}
Answer the question
In order to leave comments, you need to log in
I added a middleware that catches subdomains,
- finds if there is such a city in the database,
- if it is, skips it
- writes the city into the session
- in the controller I get the city and form the delivery of goods for this city, etc.
'domain' => '{city_alias }.website.ru', I removed
it without it, I can form links from website.ru to nsk.website.ru and vice versa
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question