1

My question of the day)
I have already implemented subdomains for cities on Laravel, but I ran into a problem!
I apologize for my english)))

I want to:

NewYork was without a subdomain website.ru
California with a subdomain California.website.ru
Texas with a subdomain Texas.website.ru
NewYork == null

I did this:
Database

alias      | name
-------------------------
null       | NewYork
california | California
texas      | Texas

Route

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

Links in blade

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 NewYork!
How can I make NewYork without a subdomain?? + less when to write.

I found a function here, maybe it's the case?
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;
    }
shaedrich
  • 5,457
  • 3
  • 26
  • 42
Den
  • 27
  • 5

1 Answers1

1

You can make the parameter optional and set a default value:

Route::group([
     'domain' => '{city_alias?}.website.ru',
     'namespace' => 'Available',
 ], function (){
     Route::get('/', 'IndexController@index')->name('index');
 });
class IndexController extends Controller 
{
    public function index($city_alias = 'NewYork')
    {
        $city_one = City::cityAlias($city_alias)->first();
        return view('........');
    }
}
shaedrich
  • 5,457
  • 3
  • 26
  • 42