How to use `Zend\Mvc\Router\Http\Hostname` with ip and port number?

时间:2017-07-10 15:26:23

标签: routing zend-framework2

For development I am use a host file override to point http://project.dev/ to my local ip.

In my module.config.php file I have (plus child routes, etc):

'router' => [
    'routes' => [
        'home' => [
            'options' => [
                'route' => 'project.dev',
            ],
        ],
    ],
],

I am now implementing a payment gateway that has a callback. How do I change this route to accept my public ip address and port number?

I have tried the simple swap:

'router' => [
    'routes' => [
        'home' => [
            'options' => [
                'route' => '12.34.56.78:1234',
            ],
        ],
    ],
],

but that doesn't work (route not found). I've swapped the port number for a variable:

            'options' => [
                'route' => '12.34.56.78:port',
                'constraints' => [
                    'port' => ':1234',
                ],
            ],

but that also did not work.

How to use Zend\Mvc\Router\Http\Hostname with ip and port number?

Is it possible to specify both in the one route?

I have another Zend\Mvc\Router\Http\Hostname route that deals specifically with subdomains:

        'subdomains' => [
            'options' => [
                'route' => ':sub.project.dev',
            ],
        ],

If the ip address is put into the route, :sub would go on the end (e.g. 12.34.56.78(:port)?/:sub). In this scenario, would it be possible to specify the domain name and ip in the one route? e.g. ':sub.project.dev|12.34.56.78(:port)?/:sub'?

1 个答案:

答案 0 :(得分:0)

Zend\Mvc\Router\Http\Hostname不打扰端口号。因此,路线可以只为'route' => '12.34.56.78',

无法添加子域。 as 子域或子文件夹。 IP的请求从根本上不包括子域组件。如果您尝试它,您会收到一条可爱的错误消息(无法访问站点,无法找到服务器DNS地址)。此路由器仅匹配请求的主机名部分,即第一个/之前的所有内容。这意味着它无法匹配请求的文件夹部分。

那么 - 我该如何解决这个问题?

对于主路由,我只是更改了ip地址并删除了端口号:

//easy enough
'router' => [
    'routes' => [
        'home' => [
            'options' => [
                'route' => '12.34.56.78',
            ],
        ],
    ],
],

由于我将使用指定的参数开发和测试它,我决定将它们作为变量的默认值输入:

'router' => [
    'routes' => [
        'subdomains' => [
            'options' => [
                'route' => '12.34.56.78',
                'defaults' => [
                    'sub' => 'abcd', //default subdomain to use.
                ],
            ],
        ],
    ],
],

<强>陷阱

使用url视图帮助程序时,指定['force_canonical' => true]将不会回显端口号,这意味着以这种方式加载的资源的任何请求都无法解析(12.34.56.78!= { {1}})。如果所有请求都是相对的,那么就没有问题了。否则,问题。

不是最好的解决方案,因为我必须在完成此功能后撤消这些更改,但希望足够好以允许支付网关在没有问题的情况下进行回调,直到我可以将此代码放到登台服务器上(其中这变为moo point)。