Vahn Marty Vahn Marty - 10 days ago 6
PHP Question

Laravel: How to redirect to subdomain using a Controller?

The function of my program is to create a subdomain/subdirectory. I have a directories table, once a user will create a subdomain it redirects to its subdomain.

Example:
My site is http://dns.dev, if I will create a subdomain 'test' and click the button 'Create directory', then it redirect to http://test.dns.dev

I have this route for subdomains.

Route::post('/create', 'DirectoryController@create');
Route::group(['domain' => '{subdomain}.dns.dev'], function () {
Route::get('/', function ($subdomain) {
return $subdomain;
});
});


In my controller,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Redirect;

use App\Directory;

class DirectoryController extends Controller
{
public function create(Request $request)
{
$directory = new Directory;
$directory->domain = $request->subdomain;
$directory->status = "pending";
$directory->save();

//redirect to $request->subdomain . '.dns.dev/'
//something like that


}
}

Answer

Try this

$url = 'http://yourdomainname.com';
return Redirect::to($url);
Comments