【发布时间】:2013-08-09 06:26:10
【问题描述】:
我正在尝试在访问 /account 路由下的任何页面时实现强制 https。我发现了这个问题ZF2 toRoute with https,它可以工作......部分。我的路线:
'router' => array(
'routes' => array(
'account' => array(
'type' => 'Scheme',
'options' => array(
'route' => '/account',
'scheme' => 'https',
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'index',
),
),
),
'signin' => array(
'type' => 'Segment',
'options' => array(
'route' => '/signin[/:type]',
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'signin',
),
'constraints' => array(
'type' => '[a-zA-Z][a-zA-Z0-9-_]*',
),
),
),
'signout' => array(
'type' => 'Segment',
'options' => array(
'route' => '/signout',
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'signout',
),
),
),
'register' => array(
'type' => 'Segment',
'options' => array(
'route' => '/register[/:step]',
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'register',
),
'constraints' => array(
'step' => '[a-zA-Z][a-zA-Z0-9-_]*',
),
),
),
),
),
),
),
以及来自 Skeleton Application 的 Application 模块的主路由(从 github 克隆)。每当我访问 /account 的任何子路由时,它都会抛出 404:
http(s)://domain.my/account/signin = 404, wrong
http(s)://domain.my/account/* = 404, wron
https://domain.my/signin = signin page, wrong should be /account/signin
http://domain.my/ = ok, main page
http://domain.my/account = 404, wrong
https://domain.my/ = wrong, account page should be main page
通常我的问题是:该页面应该通过 http 或 https 但 /account 访问,并且它的子路由只能通过 https 访问。
编辑
好的,我已经尝试了chained_routes,但这不是我想要实现的。我想做这样的事情:
用户未登录:
类型:http://domain.my/account -> 重定向到https://domain.my/account/login(我知道我可以用$authService->hasIdentity() 实现这一点)然后重定向到https://domain.my/account
类型:http://domain.my/account/login -> 重定向到 https://domain.my/account/login
类型:http://domain.my/account/edit -> 重定向到 https://domain.my/account/login 然后到 https://domain.my/account/edit
与登录用户相同,当他从 /account 路由访问任何内容时,它被重定向到相同的 url,但使用 https。
【问题讨论】:
-
对我来说,不清楚你想要什么(目标)以及你的实际结果是什么。你能简单地说我想达到 X,所以我做了 Y,但结果是 Z。你现在得到的 404 是理想的结果吗?
-
经过一番思考后,目标很简单:帐户控制器中的所有内容 - 因此以
/account开头的路由 - 必须只能通过 https 访问,当通过 http 访问它们时,它们会被重定向到 https。这既简单又复杂…… -
所以请看下面我的回答。您不应该只接受通过 https 的路由,因为您必须接受 http 版本才能执行重定向。我建议使用控制器插件,请参阅下面的示例
标签: https routes zend-framework2