현재 URL을 감지하고 로그인 페이지로 전달하고 사용자가 로그인하면 ZFCUser 모듈에 대한 기능을 구현하여 해당 URL로 리디렉션하려고합니다.로그인 후 ZFCUser가 사용자 정의 URL로 리디렉션됩니다.
누구든지이 작업을 시도 했습니까?
사람들에게 사이트의 일부 영역에 대한 링크를 제공해야하지만 사이트를보기 전에 로그인해야합니다.
감사 맥
현재 URL을 감지하고 로그인 페이지로 전달하고 사용자가 로그인하면 ZFCUser 모듈에 대한 기능을 구현하여 해당 URL로 리디렉션하려고합니다.로그인 후 ZFCUser가 사용자 정의 URL로 리디렉션됩니다.
누구든지이 작업을 시도 했습니까?
사람들에게 사이트의 일부 영역에 대한 링크를 제공해야하지만 사이트를보기 전에 로그인해야합니다.
감사 맥
당신은 설정은 설정 파일에 로그인 후 리디렉션 할 수 있습니다.
는 설정/자동로드로 파일 위https://github.com/ZF-Commons/ZfcUser/blob/master/config/zfcuser.global.php.dist
복사 /과 거기에 zfcuser.global.php
당신이 그 목적의 키 login_redirect_route을 찾을 수 있습니다 이름을 바꿉니다.
/**
* 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 번 줄을 보면 리디렉션 매개 변수를 사용할 수 있습니다. [User controller file]
if ($this->getOptions()->getUseRedirectParameterIfPresent()) {
$redirect = $request->getQuery()->get('redirect', (!empty($post['redirect'])) ? $post['redirect'] : false);
} else {
$redirect = false;
}
그런 다음, 설정 파일의 라인 137, 당신은 redirect parameter if present config을 설정해야합니다.
이 작업을 완료하면 로그인 페이지에 링크에? redirect =/whatever를 사용할 수 있습니다.
이것은 현재 작동 중이며 앞으로 며칠 동안 태그 될 것입니다.
보안 문제로 인해 toUrl 대신 toRoute를 사용하여 몇 주 전에 동작을 변경했습니다. 내가 새로운 자료를 트윗 곳
GitHub의의의 repo에 눈을 유지하거나 트위터에 나를 따르십시오 http://twitter.com/MrStroem
예, RedirectCallback.php는 일반 경로로만 리디렉션됩니다. (ZfcUser 1.2에서) 경로 매개 변수를 제공 할 수 없습니다. 그래서'asdf.com/login? redirect = zfcuser'를 할 수 있습니다. 또한 RedirectStrategy와 함께 ZfcRbac 모듈을 사용하는 경우 경로가 아닌 리디렉션 URI에 로그인하므로 제대로 전달되지 않습니다. –
그래서, 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'
공장을 재정 의하여 위의 클래스를 돌려 다음과 같이 다음과 같이, 나는 우선 내 자신의 RedirectCallback을 썼다
'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이어야하지만 권한이 없기 때문에 먼저 로그인 페이지를 통과해야합니다. – Mac