2017-05-24 43 views
0

我創建一個URL的電子郵件中發送這樣的:重定向到登錄頁面,並保持網址,以獲取參數的Symfony 2.8

$url = $this->getContainerInterface()->get('router')->generate(
    'crmpiccobundle_view_subdomain_modify', [ 
     'subdomain' => $site->getSubdomain(), 
     'subdomain_username' => $site->getSubdomain(), 
    ], UrlGeneratorInterface::ABSOLUTE_URL), 

該URL通過下面的方法在我的控制器處理:

/** 
* @Route(
*  "/view/{subdomain}/modify", 
*  name="crmpiccobundle_view_subdomain_modify" 
*) 
* 
* @Security("is_granted('IS_AUTHENTICATED_FULLY')") 
* @Template("CRMPiccoBundle:Admin/View/Modify:index.html.twig") 
* 
* @param Request $request 
* @param string $subdomain 
* 
* @return array 
*/ 
public function subdomainModifyViewAction(Request $request, string $subdomain) 
{ 

因此,如果用戶沒有登錄,路由/view/{subdomain}/modify需要認證意味着他們將被踢到登錄屏幕。我試圖實現的是通過URL中的登錄頁面(例如/login?subdomain_username=crmpicco)或其他方式將'subdomain'或'subdomain_username'(我意識到這裏有重複)傳遞到登錄頁面。

我不認爲設置會話變量是合適的,因爲腳本是由cron而不是瀏覽器中的用戶運行的。

我可以從/view/crmpicco/modify?subdomain_username=crmpicco轉到/login?subdomain_username=crmpicco嗎?

security.yml

access_control: 
    - {path: /, roles: IS_AUTHENTICATED_ANONYMOUSLY} 
    - {path: /admin, roles: ROLE_USER} 
    - {path: /admin/view, roles: ROLE_USER} 
+1

_「我不認爲,因爲腳本是由cron和沒有用戶在瀏覽器中運行設置會話變量是合適的。」 _ - 什麼,你有一個cron讀取用戶的電子郵件和點擊裏面的鏈接...? – CBroe

+0

@CBroe不,我正在使用亞馬遜的SQS隊列,我推送請求發送此電子郵件。當處理該隊列時,我的代碼會調用一個方法,併發送電子郵件。如何在進入登錄URL時維護GET參數有什麼建議嗎? – crmpicco

+0

將目標URL存儲在會話中,或將其作爲參數追加。 – CBroe

回答

0

我最終實現這一點的方法是拉目標網址,這將在成功登錄被重定向到,走出會議(_security.default.target_path),然後分析它拉URL參數輸出(?username=crmpicco)。

// pull the target URL out of the session and parse it to get the ?username=crmpicco part  

parse_str(parse_url($request->getSession()->get('_security.default.target_path'), PHP_URL_QUERY), $usernameArray); 

$prefilledUsername = isset($usernameArray['username']) ? $usernameArray['username'] : ''; 

// ... then pass through $prefilledUsername to my Login view 
相關問題