日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Symfony3实现刷新登录时间

發布時間:2025/7/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Symfony3实现刷新登录时间 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在Symfony3中實現刷新登錄時間、登錄加1的功能需要自己寫一個success handle。

簡單講一下創建success handle的流程,使用的環境如下
PHP版本:7.1.8
Symfony版本:3.3.5
默認管理員權限相關的Bundle名為AuthorizationBundle

首先我們在AuthorizationBundle下創建一個Service文件夾,用來儲存所有與service相關的文件。建立一個AuthorizationHandle.php的文件,這個是我們用來實現success handle的代碼文件。

因為實現success handle需要實現AuthenticationSuccessHandlerInterface接口中的方法,所以我們的handle類這么寫

<?phpnamespace HuanYue\AuthorizationBundle\Service;use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; use Symfony\Component\DependencyInjection\ContainerAwareTrait;class AuthorizationHandle implements AuthenticationSuccessHandlerInterface {use ContainerAwareTrait;/*** This is called when an interactive authentication attempt succeeds. This* is called by authentication listeners inheriting from* AbstractAuthenticationListener.** @param Request $request* @param TokenInterface $token** @return Response never null*/function onAuthenticationSuccess(Request $request, TokenInterface $token){$user = $token->getUser();$user->setLastLogin(new \DateTime());$user->save();return new RedirectResponse($this->container->get('router')->generate('huan_yue_admin_authorization_dashboard'));} }

下面解釋一下代碼,AuthenticationSuccessHandlerInterface中的注釋很清晰的寫明了我們可以在onAuthenticationSuccess中實現我們的功能,并且最后需要返回一個Response。我們進行刷新登錄時間、或者登錄加1、或者其他什么操作后需要跳轉到后臺首頁,因此需要生成一個RedirectResponse,我的代碼里用到了Symfony container去獲取router,所以需要使用Symfony提供給我們的Trait(注:在2.*版本中可以直接繼承ContainerAware類)。到此,我們的success handel已經實現完成。

下一步,我們把我們的AuthorizationHandle注冊到Symfony的Service中,在Resources\services.yml中添加

services:

huan_yue_authorization.authorization_handle:class: HuanYue\AuthorizationBundle\Service\AuthorizationHandlecalls:- [ setContainer, [ "@service_container" ] ]

最后一步,修改app\config\security.yml文件,在form_login下添加我們的success handle,代碼如下

form_login:check_path: huan_yue_admin_authorization_loginlogin_path: huan_yue_admin_authorization_logindefault_target_path: huan_yue_admin_authorization_dashboardsuccess_handler: huan_yue_authorization.authorization_handle

到此,整個流程完成。

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Symfony3实现刷新登录时间的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。