0

I want to get subdomains from url in laravel. Using this at now

App::before(function($request)
{
        if(Request::path() == '/' && !Request::ajax()){
        $urlParts = explode('.', $_SERVER['HTTP_HOST']);
        if($urlParts[0] == 'fasfin' || $urlParts[1] == 'fasfin' && $urlParts[0] == 'www') {Route::get('/', 'HomeController@index');} //check if url is main site
        elseif($urlParts[0] == 'www') { $subdomain = $urlParts[1]; return App::make('SubdomainController')->getIndex($subdomain);} //fix for www.subdomain.mydomain.com
        else {$subdomain = $urlParts[0]; return App::make('SubdomainController')->getIndex($subdomain);}
        } //get subdomain
});

But this is kind of very bad code, i know. Trick from official docs with {subdomain}.mydomain.com doesn't works. I enabled all subdomains from apache with

Server Alias *.mydomain.com

Update

Changed my code to this

if(!Request::ajax()){
        $isSubdomain = false;
        $urlParts = explode('.', $_SERVER['HTTP_HOST']);
        if($urlParts[0] == 'fasfin' || $urlParts[1] == 'fasfin' && $urlParts[0] == 'www') {Route::get('/', 'HomeController@index');}
        elseif($urlParts[0] == 'www') { $subdomain = $urlParts[1]; $isSubdomain = true;}
        else {$subdomain = $urlParts[0]; $isSubdomain = true;}
        if($isSubdomain)
        {
            $user = App::make('SubdomainController')->checkIndex($subdomain);
            if($user instanceof Exception) return View::make('subdomain.notExist');
            $shop = App::make('SubdomainController')->getIndex($subdomain);
            if($shop instanceof Exception) return 'Shop was already registered, but doesn't created yet';
        }
    }

At now new question is

How to pass variable to view every time? In the end i have variable $shop and want to pass it into view home.blade.php every time when it called

1

1 Answer 1

0

Found it

View::share('shop', $shop);
Sign up to request clarification or add additional context in comments.

1 Comment

if you wanty it only to besahred with one page like home you can use a composer: View::composer('home', function ($view) { view()->share('shop', $shop); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.