簡體   English   中英

PHP 中 ASP.Net 的 web.sitemap 的最佳替代品

[英]Best alternative to ASP.Net's web.sitemap in PHP

在 ASP.Net 中,我通常使用標准web.sitemap文件來驅動我的主導航,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="~/index.html" title="My Awesome Site">
        <siteMapNode url="~/About/index.html" title="About">
            <siteMapNode url="~/About/Board.html" title="Board"/>
            <siteMapNode url="~/About/Vision.html" title="Vision"/>
            <siteMapNode url="~/About/Mission.html" title="Mission"/>
            <siteMapNode url="~/About/Support.html" title="Support"/>
            <siteMapNode url="~/About/Locations.html" title="Locations"/>
            <siteMapNode url="~/About/Funding-Sources.html" title="Funding Sources"/>
            <siteMapNode url="~/About/Volunteer.html" title="Volunteer"/>
            <siteMapNode url="~/About/Staff.html" title="Staff"/>
        </siteMapNode>
        <siteMapNode url="~/Parents/index.html" title="Parents">
            <siteMapNode url="~/Parents/Child-Development.html" title="Child Development"/>
            <siteMapNode url="~/Parents/Referrals.html" title="Referrals"/>
        </siteMapNode>
        <siteMapNode url="~/Resources/index.html" title="Resources">
            <siteMapNode url="~/Resources/Resource-Library.html" title="Resource Library"/>
            <siteMapNode url="~/Resources/Links.html" title="Links"/>
        </siteMapNode>
    </siteMapNode>
</siteMap>

然后我通常為導航創建一個用戶控件,我可以在其中使用SiteMap.CurrentNode和/或SiteMap.CurrentNode.IsDescendantOf將當前頁面標記為“選定”或其他任何內容。 我正在為 PHP 尋找類似的東西。 在 ASP 經典中,我們幾乎必須使用包含和一堆if語句,但一旦你深入到幾個級別,你最終會得到一堆代碼。 我可能會在每個頁面的頂部創建變量來指定我們所在的“部分”、“子部分”和“頁面”,但如果可能的話,我想避免這種情況。

我發現這個鏈接通過查詢字符串反彈了東西,但這不是一個選項。

如果有人不熟悉web.sitemap文件,如果需要,我可以更詳細地解釋它。

編輯-@BrandonS 的評論

我正在尋找的核心是能夠定義一個代表我網站頁面的文件。 ASP.Net 使用 XML 文件,它便於捕獲一些潛在的錯誤,但不是必需的。 理想情況下,我實際上希望使用 ASP.Net 使用的完全相同的文件格式,以便我可以共享代碼,但仍然不是絕對必需的。

在 ASP.Net 中,使用上面的站點地圖,我將有一個用戶控件(基本上是一個美化的包含文件),它在每個頁面上輸出<title>標記。 主頁上,它會 output 只是My Awesome Site ,在About L2 頁面上,它會 output My Awesome Site My Awesome Site - About - Board My Awesome Site - AboutBoard L3 頁面上,它會 Z78E6221F6393D1356681DB398F14CE。 相同的想法可能用於子頁面上的面包屑。 (順便說一句,因為我所有的網站頁面都只存在一次,所以一個頁面不可能有兩個父母。)

在 ASP.Net 中,每個頁面請求都會自動創建一個 SiteMap object。 (實際上,我認為它的延遲加載意味着它實際上不會被創建,除非您嘗試訪問但這是透明地發生的。)因此,在任何給定時刻,我都可以在站點地圖文件中存在的任何頁面上使用SiteMap.CurrentNode object。 (如果站點地圖文件null中不存在該頁面,則返回。)從那里我可以要求SiteMap.CurrentNode.ParentNode或者我可以走SiteMap.CurrentNode.ChildNodes 我還可以獲得代表站點地圖文件根的SiteMap.RootNode 從那里我可以知道這些直接子節點代表我網站的第 2 級頁面,因此我可以遍歷SiteMap.RootNode.ChildNodes 然后,我可以知道每個“孫子”代表一個 L3 等等,從而單獨引導這些孩子。

因此,使用上面的遞歸,我可以通過遍歷根節點的子節點來定義我的站點的主導航,如果我的導航中有下拉菜單,那么我可以遍歷子節點。 在帶孩子或孫子走路時,我可以根據SiteMap.CurrentNode等於孩子或孫子的后代來設置類。 下面是 VB.Net 中我如何使用站點地圖構建 L2 導航的示例:

    ''//An array that we append potential CSS class names to such as "first" or "active"
    Dim Classes As List(Of String)

    ''//A StringBuild is just an efficient way to work with Strings
    ''//if you are not familiar with it just now that Append() is basically "&=" and AppenLine() is basically "&= ...\n"
    Dim Buf As New StringBuilder()
    Buf.AppendLine("     <ul>")

    ''//Walk the root child nodes, the N variable will be the current ChildNode of the SiteMap.RootNote
    For Each N As SiteMapNode In SiteMap.RootNode.ChildNodes

        ''//For the first and last child we want to add extra classes in case we need to adjust borders, padding, etc
        Classes = New List(Of String)
        If N.PreviousSibling Is Nothing Then
            Classes.Add("first")
        ElseIf N.NextSibling Is Nothing Then
            Classes.Add("last")
        End If

        ''//See if the page being requested is the current child or if the current page is descended from it so that we can mark it as active
        If SiteMap.CurrentNode IsNot Nothing Then
            If SiteMap.CurrentNode.Equals(N) OrElse SiteMap.CurrentNode.IsDescendantOf(N) Then
                Classes.Add("active")
            End If
        End If

        ''//This code below is just for outputting the <li class="..."><a href="...">...</a></li> code

        ''//Write the opening list tag
        Buf.Append("      <li")
        ''//If the above code created any CSS classes append the class attribute and the space-delimited list of classes
        If Classes.Count > 0 Then
            Buf.Append(" class=""" & Join(Classes.ToArray(), " ") & """")
        End If
        ''//Close the list tag
        Buf.Append(">")

        ''//Append the hyperlink. The "N" object is the current child of the SiteMap.RootNode.ChildNodes
        ''//All SiteMapNode objects have a Url property and a Title property which is defined in the XML file
        Buf.AppendFormat("<a href=""{0}"">{1}</a></li>", N.Url.Replace("/index.html", "/"), HttpUtility.HtmlEncode(N.Title))

        ''//Append a blank line, I like to keep my code formatted nicely
        Buf.AppendLine()
    Next

    ''//Append the closing list tag
    Buf.AppendLine("     </ul>")

附加說明

有些人可能認為這有點矯枉過正,但這都是內置在.Net中的,這就是為什么我要問PHP是否存在某些東西。 我可以自己動手,但顯然我想盡可能避免。

嗯...據我所知,PHP 中沒有內置解決方案。 它們存在於許多框架中。

例如,在 Zend 中,我們有導航容器,它可以以您選擇的任何格式存儲所有數據——例如 xml-config。 然后在zend 視圖導航助手的幫助下,我們可以在我們的布局或具體頁面中 output 它。

例如,您可以在此處看到關於它的自動翻譯帖子。

為了澄清我是否正確理解了您的目標:您是否想要一個文件來處理所有頁面的標題和父級?

當然:htaccess :)

RewriteEngine on
# ROOT
RewriteRule ^me.html$           me.php?title=Home+Page [L,QSA]
# ABOUT
RewriteRule ^about/index.html$  about/index.php?parent=About&title=About [L,QSA]
RewriteRule ^about/staff.html$  about/staff.php?parent=About&title=The+Staff [L,QSA]

但是,您不能在文件系統結構中使用.html 文件,因為它是 static。 這些文件需要在.php 中,但是通過該 htaccess 的配置,URL 將保留在.html。

URL about/index.html將查找about/index.php並向其傳遞兩個變量:父級和標題。 The php files will also need to be organized according to your URL directory like index.php , staff.php in /about folder.

about/index.php文件中,可以output:

<?php
echo $_GET['parent'];
echo "<br />";
echo $_GET['title'];

如果我的答案不充分,請對 htaccess 進行更多研究,但我認為這就是您要尋找的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM