Php 登录后ZFCUser重定向到自定义URL
我正在尝试为ZFCUser模块实现一个函数,该函数将检测当前URL,将其传递到登录页面,并在用户登录后将其重定向到该URL 有人试过这样做吗 我需要向人们提供指向网站某些区域的链接,但在他们看到这些链接之前,必须强迫他们登录 谢谢Php 登录后ZFCUser重定向到自定义URL,php,zend-framework,zend-framework2,zfcuser,Php,Zend Framework,Zend Framework2,Zfcuser,我正在尝试为ZFCUser模块实现一个函数,该函数将检测当前URL,将其传递到登录页面,并在用户登录后将其重定向到该URL 有人试过这样做吗 我需要向人们提供指向网站某些区域的链接,但在他们看到这些链接之前,必须强迫他们登录 谢谢 Mac您可以在登录配置文件后设置重定向 将上述文件复制到config/autoload/中,并将其重命名为zfcuser.global.php 在那里,您可以找到用于此目的的键登录\重定向\路由 /** * Login Redirect Route * * U
Mac您可以在登录配置文件后设置重定向 将上述文件复制到config/autoload/中,并将其重命名为zfcuser.global.php 在那里,您可以找到用于此目的的键登录\重定向\路由
/**
* Login Redirect Route
*
* Upon successful login the user will be redirected to the entered route
*
* Default value: 'zfcuser'
* Accepted values: A valid route name within your application or a callback.
* If callback used, it will receive the identity as the param
*
*/
'login_redirect_route' => '/your-url',
如果查看用户控制器的第83行,您将看到可以使用重定向参数 然后,在配置文件的第137行,您必须设置
完成后,您可以在登录页面的链接中使用?redirect=/任何内容。这项工作正在进行中,可能在未来几天内会被标记 出于安全考虑,我们在几周前改变了行为,因此它使用了toRoute而不是toUrl 请关注github repo或关注我在twitter上发布的新版本:因此,由于ZfcUser(1.2)只允许重定向到普通路由(无参数,无URI),我编写了自己的重定向回调来覆盖,如下所示:
<?php
namespace Application\Controller;
use Zend\Mvc\Application;
use Zend\Mvc\Router\RouteInterface;
use Zend\Mvc\Router\Exception;
use Zend\Http\PhpEnvironment\Response;
use ZfcUser\Options\ModuleOptions;
use Zend\Mvc\Router\RouteMatch;
use Zend\Http\Request;
/**
* Can redirect to the URI given during login.
* N.B. Don't extend ZfcUser version since its members are all private.
*/
class RedirectUriCallback {
/**
* @var RouteInterface
*/
protected $router;
/**
* @var Application
*/
protected $application;
/**
* @var ModuleOptions
*/
protected $options;
/**
* @param Application $application
* @param RouteInterface $router
* @param ModuleOptions $options
*/
public function __construct(
Application $application, RouteInterface $router, ModuleOptions $options
) {
$this->router = $router;
$this->application = $application;
$this->options = $options;
}
/**
* @return Response
*/
public function __invoke() {
$routeMatch = $this->application->getMvcEvent()->getRouteMatch();
$redirect = $this->getRedirect($routeMatch->getMatchedRouteName(),
$this->getRedirectRouteFromRequest());
$response = $this->application->getResponse();
$response->getHeaders()->addHeaderLine('Location', $redirect);
$response->setStatusCode(302);
return $response;
}
protected function getRedirectRouteFromRequest() {
$request = $this->application->getRequest();
$redirect = $request->getQuery('redirect')? : $request->getPost('redirect');
return $this->getSafeRedirect($redirect);
}
/**
* Sanitize URI: through router and back.
* @param $uri
* @return mixed
*/
private function getSafeRedirect($uri) {
if (!$uri) {
return false;
}
try {
$request = new Request;
$request->setUri($uri);
$routeMatch = $this->router->match($request);
if ($routeMatch instanceof RouteMatch) {
return $this->router->assemble(
$routeMatch->getParams(),
['name' => $routeMatch->getMatchedRouteName()]
);
}
} catch (Exception\RuntimeException $e) {
}
return null;
}
/**
* Returns the url to redirect to based on current route.
* If $redirect is set and the option to use redirect is set to true, it will return the $redirect url.
*
* @param string $currentRoute
* @param mixed $redirect
* @return mixed
*/
protected function getRedirect($currentRoute, $redirect = null) {
$redirect = $this->getSafeRedirect($redirect);
if (!$this->options->getUseRedirectParameterIfPresent()) {
$redirect = false;
}
if ($redirect) {
return $redirect;
}
$route = 'zfcuser';
switch ($currentRoute) {
case 'zfcuser/register':
case 'zfcuser/login':
$route = $this->options->getLoginRedirectRoute();
break;
case 'zfcuser/logout':
$route = $this->options->getLogoutRedirectRoute();
break;
}
return $this->router->assemble([], ['name' => $route]);
}
}
'zfcuser_redirect_callback' => function ($sm) {
$router = $sm->get('Router');
$application = $sm->get('Application');
$options = $sm->get('zfcuser_module_options');
return new RedirectUriCallback($application, $router,
$options);
},
(祝你好运,不一定。)谢谢,但我不能硬编码。它必须是用户将要访问的URL,但由于他们没有权限,他们需要首先浏览登录页面;您不能(从ZfcUser 1.2开始)提供管线参数。因此,您可以执行
asdf.com/login?redirect=zfcuser
。同样值得注意的是,如果使用带有重定向策略的ZfcRbac模块,则会传递到登录重定向URI(而不是路由),因此它们没有正确地传递。
'zfcuser_redirect_callback' => function ($sm) {
$router = $sm->get('Router');
$application = $sm->get('Application');
$options = $sm->get('zfcuser_module_options');
return new RedirectUriCallback($application, $router,
$options);
},