Laravel-CNAME +子域路由 [英] Laravel - CNAME + Subdomain Routing

查看:108
本文介绍了Laravel-CNAME +子域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路线设置如下:

<?php

Route::group([
    'domain' => '{username}.u.'.env('APP_DOMAIN'),
], function () {
    Route::get('/', 'FrontendController@site');
});

Route::group([
    'domain' => env('APP_DOMAIN'),
], function () {
    // Regular site routes
});

Route::group([
    'domain' => '{domain}',
], function () {
    Route::get('/', 'FrontendController@domain');
});

我要实现的目标是允许用户拥有自己的网站,例如hello.u.domain.com,并且这些站点也可以通过CNAME到其子域的自定义域进行服务.使用上面的路由,通配符子域可以很好地工作.但是,自定义域路由永远不会受到影响.每当访问被CNAME命名为子域的自定义域时,都会使用常规站点路由.

What I'm trying to achieve is allowing users to have their own sites, e.g. hello.u.domain.com, and for those sites to also be served through a custom domain that is CNAME'd to their subdomain. Using the routing above, the wildcard subdomain works perfectly fine. However the custom domain routing is never hit; whenever a custom domain that is CNAME'd to the subdomain is visited, the regular site routes are used.

APP_DOMAIN与自定义域不同,并且我的RouteServiceProvider.php中有$router->pattern('domain', '[a-z0-9.]+');,允许{domain}作为完整域名.

APP_DOMAIN is not the same as the custom domain, and I have $router->pattern('domain', '[a-z0-9.]+'); in my RouteServiceProvider.php to allow {domain} as a full domain name.

推荐答案

不幸的是,我没有得到我的评论的答案.该答案假定问题是使用正常的默认路由而不是具有子域的路由.

Unfortunately I did not get an answer to my comment. This answer assumes the problem is that normal default routes are used instead of routes with subdomain.

示例:

用户正在访问sub.website.com,但是route()返回website.com/blabla而不是sub.website.com/blabla

A user is visiting sub.website.com but route() returns website.com/blabla instead of sub.website.com/blabla

您可以通过动态地在route.php中创建域模式来解决此问题

You can solve this by dynamically create the pattern for domain inside routes.php

// routes.php
$url_parameters = @explode(".", $_SERVER['HTTP_HOST']);

if (count($url_parameters) == 3)
{
    $pattern = '{subdomain}.{domain}.{tld}';
}
else
{
    $pattern = '{domain}.{tld}';
}

Route::group(['domain' => $pattern], function () {

    Route::get('/', [
        'as' => 'get_index',
        'uses' => 'HomeController@getIndex'
    ]);

}

使用此方法,将导致route()和控制器参数出现问题.

By using this method you will create a problem with your route() and controller parameters.

route()问题

在使用此方法时调用route()函数时,您将得到缺少的参数错误. route()函数希望您提供{subdomain}.{domain}.{tld}参数.

When calling the route() function while using this method you will get a missing argument error. The route() function expects you to give the {subdomain}.{domain}.{tld} parameters.

您可以通过创建自己的路由功能来解决此问题.我将其命名为mdroute()(多域路由).

You can solve this by creating your own route function. I named it mdroute() (multi domain route).

function mdroute($route, $parameters = [])
{

    $data = [
        'domain' => \Request::route()->domain,
        'tld' => \Request::route()->tld
    ];


    $subdomain = \Request::route()->subdomain;

    if ($subdomain) $data['subdomain'] = $subdomain;

    // You can use mdroute('blabla', 'parameter')
    // or mdroute('blabla', ['par1' => 'parameter1', 'par2' => 'parameter2'])
    //
    if (is_array($parameters))
    {
        $data = array_merge($data, $parameters);
    }
    else
    {
        $data[] = $parameters;
    }


    return route($route, $data);

}

控制器问题

参数{sub}.{domain}.{tld}始终发送到您的控制器.您无法像往常一样访问其他参数.

The parameters {sub}.{domain}.{tld} are always send to your controller. You can't access other parameters the way you are used to be.

示例:

// Domain = sub.webite.com


// Your route
//
Route::get('/post/{id}/{param2}', [
    'uses' => 'PostController@getIndex'
]);

// PostController
//
public function getIndex($id, $param2)
{

    // $id will be 'sub'
    // $param2 will be 'website'

}

您可以通过通过Request对象访问参数来解决此问题.

You can solve this by accessing your parameter through the Request object.

public function getIndex(Request $request)
{

    $id = $request->id;

    $param2 = $reqeust->param2;

}

这篇关于Laravel-CNAME +子域路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆