問題描述
在編寫 PHP 代碼時,我決定從意大利面條式代碼轉向嘗試實現 MVC.為了實現MVC框架,我發泄一下這篇文章文章開了個好頭,我設法創建了我的網站,并開發了前端.現在,我正在嘗試使用會話和其他會員區功能來實現后端.我的大腦因新信息而沸騰,我的問題多于答案.
While coding PHP, I decided to move from spaghetti code and try to implement MVC. To implement the MVC framework, I vent to this article Article gave a good start and I managed to create my site, and develop the front-end. Now, I am trying to implement the back-end using sessions and other member-area features. My brain is boiling with new information and I have more questions than answers.
我不知道如何實現額外的類,例如 user
類.例如,如果沒有 MVC,我可以在我的包含目錄中創建新的 user.php
類文件,然后包含它,實例化它,并為對象分配適當的值并將 objest 放入會話中.
I don't know how to implement additional classes, user
class for example.
For example, without MVC I could create new user.php
class file in my includes directory, then include it, instantiate it, and assign appropriate values to the object and place objest into session.
我想尋求專家建議.
我對很多事情感到困惑:
I am confused about many things:
- 在哪里添加用戶類
- 如何在我的 MVC 中添加和包含用戶類
- 如何在我的應用程序中攜帶用戶類(我在會話中理解,但會話必須具有用戶對象的權限
- 我如何執行登錄/注銷邏輯并在后臺執行所需的操作
這可能是一個常見的問題,一旦解決過就不復雜了.我也為 不是很好定義的問題
道歉,但我提前感謝您的幫助.
It is probaly a common problem that is not complex once it has been done before. I also apologize for not very good defined question
, but I appreciate your help in advance.
推薦答案
User
,MVC 的上下文,是一個 域對象.然而,會話是一種存儲介質(就像緩存、數據庫或文件系統).當您需要在那里存儲來自 User
實例的數據時,您可以使用某種類型的 數據映射器 去做.
User
, context of MVC, is a domain object. However the session is a form of storage medium (just like cache, db or file-system). When you need to store data from User
instance there, you use some type of data mapper to do it.
$user = $this->domainObjectFactory->build('user');
$user->setName('Korben')
->setSurname('Dallas');
if ( some_condition )
{
$mapper = $this->dataMapperFactory->create('session');
$mapper->store($user);
}
此代碼應該為會話和用戶之間的交互提供一個極其簡化的示例.
This code should provide an extremely simplified example for interaction between session and user.
作為域對象,User
實例應該在 中使用服務 并使用工廠進行初始化.在 MVC 的上下文中,服務是模型層中的結構,它處理應用程序邏輯.它們操縱和促進領域對象和存儲抽象的交互.
As a domain object, the User
instances should be used inside services and initialized using factories. In context of MVC, the services are structures in model layer, which deal with application logic. They manipulate and facilitate the interaction of domain object and storage abstractions.
您的所有類都應該使用自動加載器添加.您應該閱讀關于 spl_autoload_register()
,最好同時使用 命名空間.
All of your classes should be added using autoloader. You should read about use of spl_autoload_register()
, preferably while using namespaces.
實例本身的初始化應該由工廠完成.這使您可以將代碼與所述實例的類名分離.
The initialization of instance itself should be done by a factory. That lets you decouple your code from the class name of said instance.
你沒有.
PHP 應用程序不會持久化.你有一個 HTTP 請求,你用它做所有你需要的事情,響應被發送,應用程序被銷毀.User
類的實例都將是短暫的.
PHP applications do not persists. You have an HTTP request, yo do all the things you need with it, the response is sent and application is destroyed. The instances of User
class will all be short-lived.
要在請求之間恢復當前用戶,請在會話中存儲標識符.不要在會話中轉儲整個對象.相反,在您從會話中獲取用戶標識符后,您可以從其他形式的存儲中恢復其余的用戶帳戶詳細信息(如果您甚至需要的話).
To recover the current user between requests you store an identifier in session. Do not dump whole objects in session. Instead, after you fetch user's identifier from session, you can recover the rest of user account details from other forms of storage (if you even need it).
這整個過程應該由某種識別服務"執行和管理;或認證服務"來自您的模型層.
This whole process should be preformed and managed by some sort of "recognition service" or "authentication service" from your model layer.
登錄請求首先由控制器處理:
The login request is at first handled by controller:
public function postLogin( $request )
{
$service = $this->serviceFactory->create('recognition');
$service->authenticate( $request->getParameter('username'),
$request->getParameter('password') );
}
該服務嘗試驗證用戶的憑據,這會改變模型層的狀態.然后,視圖實例會查找該狀態,并將您作為經過身份驗證的用戶重定向到登錄頁面,或者將您重定向回帶有錯誤消息的登錄頁面.
The service tries to verify the user's credentials, which alters the state of model layer. The view instance then looks up that state and either redirects you to the landing page as authenticated user, or redirects you back to login page with an error message.
服務本身將通過工廠在模型控制器和視圖之間共享.這意味著他們只會初始化每個服務一次,然后就可以重用它.類似的東西:
The service themselves would be shared between model controller and view via the factory. Which means that they would initialize each service only once and then just reuse it. Something along the lines of:
class ServiceFactory
{
private $cache = array();
public function create( $name )
{
if ( array_key_exists($name, $this->cache) === false )
{
$this->cache[$name] = new $name;
}
return $this->cache[$name];
}
}
請記住,這是一個極其簡化的示例.
Just keep in mind that his is an extremely simplified example.
要進一步閱讀,我建議您瀏覽此鏈接集.此外,您可能會發現這 3 個帖子有些用處:this、這個 和這個.
For further reading I would recommend you to go through this collection of links. Also, you might find these 3 posts somewhat useful: this, this and this.
這篇關于熟悉 MVC - 我如何使用會話邏輯、附加類和后臺邏輯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!