T
T
turindd2021-07-09 07:33:51
Laravel
turindd, 2021-07-09 07:33:51

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
60e7d0455ecda079535043.jpeg
Routes

Route::group([
     'domain' => '{city_alias}.website.ru',
     'namespace' => 'Available',
 ], function (){
     Route::get('/', '[email protected]')->name('index');
 });

Links in blades
route('category.one', [$city_one->alias, $category_one->alias])

Controller
class IndexController extends Controller 
{
    public function index($city_alias)
    {
        $city_one = City::cityAlias($city_alias)->first();
        return view('........');
    }
}


The problem is with the city of MOSCOW! moscow = null. How to make Moscow without a subdomain? + less when to write.
Perhaps give 2 tips:
- how to do it in a simple way (a lot of code through if)
- how to do it in a complex way (inheritance and constructors)

Thanks in advance to everyone !!! Whatever the answer is!!!

Problem with 'dot', I even tried to add 'dot' => nsk to alias in the database.
and remove the dot in the routes)) but alas)) Maybe it can be removed differently.

Perhaps something needs to be changed here:
Illuminate\Routing\Route.php
/**
     * 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

1 answer(s)
T
turindd, 2021-07-14
@turindd

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 question

Ask a Question

731 491 924 answers to any question