問題描述
我的目標是要求某些頁面登錄.我正在使用 Zend Framework MVC,并且正在嘗試查找有關最佳實踐的示例.
My goal is to require login for certain pages. I am using Zend Framework MVC, and I'm trying to find examples regarding best practices.
關于我正在尋找的內容的一些說明:
Some notes on what I'm looking for:
- 我希望未登錄的用戶獲得一個登錄框,然后在通過身份驗證后返回到登錄版本的頁面
- 我想使用依賴注入,避免單例
- 代碼占用空間小 - 與 Zend mvc 結構相結合
- 登錄框應該是一個單獨的控制器并進行標頭重定向嗎?認證成功后如何返回登陸頁面?簡單地調用登錄控制器操作以在登錄頁面中顯示登錄框的想法,或者這是否是搜索引擎索引的缺點?
- 能夠使用外部庫來處理 cookie
或者完全不同的東西.我對 Zend 框架還很陌生,我想以正確的方式"來做.
Or something completely different. I'm fairly new to the Zend framework, and I want to do it 'the right way'.
推薦答案
- 我想讓非登錄用戶得到一個登錄框,然后返回到登錄狀態在頁面的版本中,一次已認證
使用 FrontController 插件 并重定向或轉發它們到您的登錄操作.
Use a FrontController plugin and redirect or forward them to your loginAction.
- 我想使用依賴注入,避免單例
Zend 框架,目前不提供任何 DI 系統,但是 Zend_Application_Resource_* 實際上取代了它.這里需要什么樣的依賴?
Zend Framework, doesn't currently ship any DI system, however, the Zend_Application_Resource_* actually replace it. What kind of dependency would you need here?
- 代碼占用空間小 - 與 Zend mvc 結構相結合
這取決于你.
- 登錄框應該是一個單獨的控制器并進行標頭重定向嗎?如何認證后返回登陸頁面成功?一個簡單地調用的想法登錄控制器操作以顯示登陸頁面中的登錄框,或者這是搜索的缺點引擎索引?
我主要使用一個特殊的 AuthController
和 LoginAction
&注銷操作
.為了將用戶重定向到試圖查看的頁面,我總是在我的表單中添加一個 returnUrl
元素,并注入請求的 URL 的值以便能夠重定向用戶,如果沒有,我將他重定向到索引/儀表板,這取決于.
I mostly use a special AuthController
with LoginAction
& LogoutAction
. To redirect the user to the page is was trying to view, I always add a returnUrl
element in my forms, and I inject the value of the requested URL to be able to redirect the user, and if none, I redirect him to the index/dashboard, depends.
- 能夠使用外部庫來處理 cookie
Zend_Auth 允許您設置您的 自己的存儲機制,所以只需要實現接口即可.
Zend_Auth allows you to set your own storage mechanism, so just implement the interface.
$auth = Zend_Auth::getInstance();
$auth->setStorage(new My_Auth_Storage());
但是從不將身份驗證結果存儲在 cookie 中,修改它并訪問您的網站非常容易.
But never store authentication result in a cookie, it's so easy to modify it and access your website.
您還可以查看我的一個之前的答案.
You may also take a look to one of my previous answer.
這篇關于Zend_Auth 最佳實踐的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!