A route using either a prefix or a domain

I am working on a platform that allows users to run their own website either in a subfolder of the main domain of the website, or to configure their own domain for their website.

When using a custom domain, the URL structure for each route is somewhat different in that it has a username prefix, but when using a custom domain, this prefix is ​​not used.

Is there any reasonable way to achieve this in my Route :: group to handle both types of requests in the same route and successfully use reverse routing to create the appropriate URL based on the parameters passed to it.

Below is an example of using the prefix

Route::group(array( 'prefix' => 'sites/{username}'), function() {
    Route::get('/photos/{album_id}.html', array('uses' => 'Media\PhotosController@album_view', 'as' => 'photo_album'));
});

And here is an example of using a custom domain

Route::group(array('domain' => '{users_domain}'), function() {
    Route::get('/photos/{album_id}.html', array('uses' => 'Media\PhotosController@album_view', 'as' => 'photo_album'));
});

,

route('photo_album', ['username' => 'johnboy', 'album_id' => 123] )

http://www.mainwebsitedomain.com/sites/johnboy/photos/123.html

route('photo_album', ['users_domain' => 'www.johnboy.com', 'album_id' => 123] )

http://www.johnboy.com/photos/123.html
+4
1

, ...

. , ,

, , .

, " ":

Route::group(array( 'prefix' => 'sites/{username}'), function() {
    Route::get('/photos/{album_id}.html', array('uses' => 'Media\PhotosController@album_view',
                                                'as' => 'photo_album'));
});

Route::group(array('domain' => '{users_domain}'), function() {
    Route::get('/photos/{album_id}.html', array('uses' => 'Media\PhotosController@album_view',
                                                'as' => 'domain.photo_album'));
});

, URL-:

route('photo_album', ['username' => 'johnboy', 'album_id' => 123] )
route('domain.photo_album', ['users_domain' => 'www.johnboy.com', 'album_id' => 123])

( , domain. URL- ...)


, Laravel , 'domain' => '{users_domain}'. URL-, , 404. ? , , . .

. config/app.php:

'domain' => env('APP_DOMAIN', 'www.mainwebsitedomain.com')

, .

:

$currentDomain = Request::server('HTTP_HOST');
if($currentDomain != config('app.domain')){
    Route::group(array('domain' => $currentDomain), function() {
        Route::get('/photos/{album_id}.html', array('uses' => 'Media\PhotosController@album_view',
                                                    'as' => 'current_domain.photo_album'));
    });
}

Soooo... . , . , ( ) . :

app/Http/routes/photos.php:

if(!empty($routeNamePrefix)){
    $routeNamePrefix = $routeNamePrefix . '.';
}
else {
    $routeNamePrefix = '';
}

Route::get('/photos/{album_id}.html', ['uses' => 'Media\PhotosController@album_view',
                                            'as' => $routeNamePrefix.'photo_album']);

routes.php:

// routes for application domain routes
Route::group(['domain' => config('app.domain'), 'prefix' => 'sites/{username}'], function($group){
    include __DIR__.'/routes/photos.php';
});

// routes to LISTEN to custom domain requests
$currentDomain = Request::server('HTTP_HOST');
if($currentDomain != config('app.domain')){
    Route::group(['domain' => $currentDomain], function(){
        $routeNamePrefix = 'current_domain';
        include __DIR__.'/routes/photos.php';
    });
}

// routes to GENERATE custom domain URLs
Route::group(['domain' => '{users_domain}'], function(){
    $routeNamePrefix = 'domain';
    include __DIR__.'/routes/photos.php';
});

, , URL-. , Laravel route() , . , app/helpers.php bootstrap/autoload.php vendor/autoload.php:

require __DIR__.'/../app/helpers.php';

require __DIR__.'/../vendor/autoload.php';

helpers.php:

function route($name, $parameters = array(), $absolute = true, $route = null){
    $currentDomain = Request::server('HTTP_HOST');
    $usersDomain = array_get($parameters, 'users_domain');
    if($usersDomain){
        if($currentDomain == $usersDomain){
            $name = 'current_domain.'.$name;
            array_forget($parameters, 'users_domain');
        }
        else {
            $name = 'domain.'.$name;
        }
    }
    return app('url')->route($name, $parameters, $absolute, $route);
}

, , route() .

+5

All Articles