久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我可以從視圖調用模型嗎?

Can I call a Model from a View?(我可以從視圖調用模型嗎?)
本文介紹了我可以從視圖調用模型嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我沒有使用成熟的 PHP MVC,而是設計一種最適合我的用途的.我已經完成了基本框架,并編寫了運行網站所需的模型和控制器.

現在我要轉向 Views,但我遇到了一個小難題.我的方法對我來說效果很好,但為了將來參考,我想知道我正在做的事情是否是一個壞習慣.

我想做什么:

在我的視圖中,我正在調用一個運行我的身份驗證系統的模型,并請求用戶的登錄狀態.然后我使用該布爾值來決定是否在視圖中顯示某些元素,以及將其他元素放置在哪里.

我應該為每個登錄狀態設計單獨的視圖,還是這種方法合適?但是,如果我要將這個 MVC 實施到我為我的客戶所做的工作中,我需要使用最佳實踐.

任何建議將不勝感激!

解決方案

我可以從視圖中調用模型嗎?

是的,您可以.只要您保持 M、V 和 C 之間的關注點分離,您就可以自由調用模型(或控制器) 從視圖.大多數 MVC 圖至少顯示了視圖和模型之間的雙向連接.但是,您不想做的是將邏輯/代碼從模型(或控制器)放入視圖中,并且您不想從那里修改模型.

例如,您的頁面上可能有一個小部件,用于匯總網站每個頁面上您最喜歡的博客的最新十篇博客文章標題.您可以通過在模型中調用 MyFavFeeds::getLatest(); 來獲取頭條新聞.你現在有什么選擇?

  1. 可以添加代碼以將標題提取到控制器中,但這需要您在每個控制器操作中復制它,這違反了 DRY 原則.此外,控制器關注的是處理特定操作的用戶輸入,并且獲取每次調用的標題甚至可能與這些操作無關.
  2. 如果您的架構支持它,您可以在某種 preDispatch 掛鉤中獲取該數據,也就是說,標題從插件或回調中加載并注入到視圖中.這將是 DRY,但第二個開發人員可能不知道該插件并意外地覆蓋了他的控制器操作中包含標題的變量.在某些情況下,您可能不想加載標題,例如當僅呈現表單提交的確認頁面時,因此您必須具有禁用插件的機制.這需要考慮很多.
  3. 您將對 MyFavFeeds::getLatest() 的調用(而不是其代碼)放入 View 或 Layout 模板,或者更好的 ViewHelper,它封裝了對模型類的調用并呈現小部件.這樣您就不必擔心覆蓋任何變量或重復.當您的視圖中不需要標題時,您只需不包含它即可.

關于您的其他問題:

<塊引用>

在我看來,我正在調用一個模型運行我的身份驗證系統,以及請求用戶的登錄狀態.然后我使用那個布爾值來決定是否顯示某些元素在視圖中,以及放置的位置其他.

身份驗證是您希望在應用程序流程的早期,在調用任何控制器操作之前進行的事情.因此,您不應在視圖中運行您的(整個)身份驗證系統.實際的身份驗證不是與 View 相關的邏輯.另一方面,僅在身份驗證后請求用戶狀態是可以的.例如,如果您想呈現一個顯示用戶名并提供登錄/注銷按鈕的小部件,那么執行類似

如果您的 GUI 的大部分要由用戶的角色修改,您可能希望將您的視圖分成部分塊并根據狀態包含它們,而不是將所有 HTML 寫入視圖助手.

如果您只想根據用戶角色呈現導航,請查看 Zend Framework 的 Zend_NavigationZend_Acl 以了解它們是如何實現的.

Rather than use a full-blown PHP MVC, I'm designing one that will best-fit my uses. I have the basic framework done, and have coded the models and controllers I'll need to run my website.

Now I'm moving onto the Views, and I've encountered a small dilemma. My approach is working fine for me, but for future reference, I want to know if what I'm doing is a bad habit to get into.

What I'm trying to do:

In my View, I'm calling a Model that runs my authentication system, and requesting the login status of a user. I then use that boolean to decide whether to show certain elements within the view, and where to place others.

Should I be designing separate views for each login status, or is this approach fine? However, if I'm going to be implementing this MVC into the work I'm doing for my clients, I need to use the best practices.

Any advice would be appreciated!

解決方案

Can I call the model from the View?

Yes, you can. As long as you maintain the separation of concerns between M,V and C, you are free to call upon the Model (or the Controller) from the View. Most MVC diagrams show a bidirectional connection at least between View and Model. What you don't want to do though, is place logic/code from the Model (or the controller) into the View and you don't want to modify the model from there.

For example, you might have a widget on your page that aggregates the latest ten blog posts headlines from your favorite blogs on each page of your website. You get the headlines by calling, say MyFavFeeds::getLatest(); in your model. What are your options now?

  1. You could add the code to fetch the headlines into the controller, but that would require you to replicate it in each and every controller action, which is against the DRY principle. Also, the controller's concern is handling user input for specific actions and fetching the headlines on each call is likely not even related to these actions.
  2. If your architecture supports it, you could fetch that data in some sort of preDispatch hook, that is, the headlines get loaded and injected into the View from a plugin or callback. That would be DRY, but a second developer might not be aware of that plugin and accidently overwrite the variable holding the headlines from his controller action. And there might be cases in which you wouldn't want to load the headlines, e.g. when just rendering confirmation pages for form submissions, so you'd have to have mechanism for disabling the plugin then. That's a lot to consider.
  3. You place the call to (not the code of) MyFavFeeds::getLatest() into the View or Layout template or, better, a ViewHelper, that encapsulates the call to your model class and renders the widget. This way you don't have to worry about overwriting any variables or repetition. And when you don't need the headlines on your view, you simply don't include it.

About your other question:

In my View, I'm calling a Model that runs my authentication system, and requesting the login status of a user. I then use that boolean to decide whether to show certain elements within the view, and where to place others.

Authentication is something you will want to do early in the application flow, before any controller actions are called. Thus, you should not run your (entire) authentication system in the View. The actual authentication is not View-related logic. Just requesting the user status after authentication, on the other hand, is okay. For instance, if you want to render a widget showing the user name and giving a login/logout button, it would be fine to do something like

<?php //UserHelper
class UserMenuHelper
{
    public function getUserMenu()
    {
        $link = '<a href="/user/logout">Logout</a>';
        if(MyAuth::userHasIdentity()) {
           $link = sprintf('<a href="/user/logout">Logout %s</a>',
                            MyAuth::getUsername());
        }
        return $link;
    }
}

If you got larger portions of your GUI to be modified by a User's role, you might want to break your View apart into partial blocks and include them based on the status, instead of writing all the HTML into a View Helper.

If you are only looking to render a navigation based on the user role, have a look at Zend Framework's Zend_Navigation and Zend_Acl to see how they do it.

這篇關于我可以從視圖調用模型嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 午夜看片网站 | 国产成人99久久亚洲综合精品 | 成人性视频免费网站 | 色狠狠桃花综合 | 一区二区三区国产好 | 91啪亚洲精品 | 国产精品视频免费观看 | 久久久久久久国产精品影院 | 综合第一页 | 91精品国产综合久久精品 | 精品日韩一区 | 亚洲免费在线视频 | 国产欧美在线观看 | 在线看亚洲 | 欧美日韩综合精品 | 在线国产视频观看 | 中文字幕一区二区不卡 | 亚洲精品99999 | 五月婷婷导航 | 欧美精品一区在线 | 97视频在线观看网站 | 国产精品久久久久aaaa九色 | 国产成人在线一区 | 亚洲一区二区三区 | 午夜精品| 亚洲一区在线免费观看 | 97精品一区二区 | 中文字幕高清 | 国产日产欧产精品精品推荐蛮挑 | 黄色网址在线免费播放 | 国产精品美女久久久久久不卡 | 色黄视频在线 | www.久久.com| 亚洲视频在线观看一区二区三区 | 中文字幕亚洲视频 | 91在线色视频 | 日韩精品成人一区二区三区视频 | 久久久久久久一区二区 | 综合网中文字幕 | 婷婷成人在线 | 天天干夜夜操视频 |