简体   繁体   中英

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

In ASP.Net I usually drive my primary navigation using a standard web.sitemap file such as the following:

<?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>

Then I usually create a user control for the navigation where I can use SiteMap.CurrentNode and/or SiteMap.CurrentNode.IsDescendantOf to mark the current page as "selected" or whatever. I'm looking for something similar for PHP. In ASP classic we would pretty much have to use an include and a bunch of if statements which works but once you get a couple of levels deep you end up with a bunch of code. I could probably create variables at the top of each page designating which "section", "subsection" and "page" we're on but I'd like to avoid that if possible.

I found this link which bounces things through the querystring but that's not an option.

If anyone's not familiar with the web.sitemap file I can explain it in more detail if needed.

EDIT - Comments for @BrandonS

The core of what I'm looking for is to be able to define a single file that represents the pages of my site. ASP.Net uses an XML file which is convenient for catching some potential errors but isn't required. Ideally I'd actually like to use exact same file format used by ASP.Net so that I can share code but still not absolutely required.

In ASP.Net, using the sitemap above I would have a user control (basically a glorified include file) that outputs the <title> tag on every page. On the Home page it would output just My Awesome Site , on the About L2 page it would output My Awesome Site - About and on the Board L3 page it would output My Awesome Site - About - Board . The same idea might be used for breadcrumbs on subpages. (Quick aside, for all of my sites pages exist only once so there's no chance of a page having two parents.)

In ASP.Net on every page request a SiteMap object gets automatically created. (Actually I think its lazy-loaded meaning it actually doesn't get created unless you try accessing but that happens transparently.) So at any given moment I can use a SiteMap.CurrentNode object on any page that exists in the sitemap file. (If the page doesn't exist in the sitemap file null is returned.) From that I can ask for the SiteMap.CurrentNode.ParentNode or I can walk the SiteMap.CurrentNode.ChildNodes . I can also get the SiteMap.RootNode which represents the root of the sitemap file. From that I can walk the SiteMap.RootNode.ChildNodes knowing that these direct children represent the Level 2 pages of my site. I can then walk these children individually knowing that each "grandchild" represents an L3 and so on.

So using the above recursion I could define my site's primary navigation by walking the root node's children and if I've got dropdown menus in my nav then I can walk the sub-children. While walking the children or grandchildren I can set classes based on if the SiteMap.CurrentNode equals or is descended from the child or grandchild. Below is an example in VB.Net of how I use the sitemap for building an L2 navigation:

    ''//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>")

Additional note

Some people might think that this is overkill but this is all built into.Net, that's why I'm asking if something exists for PHP. I could roll my own but obviously I'd like to avoid it if possible.

Hmm... As I know, there are no built-in solutions in PHP. They are in a plenty of frameworks.

For example in Zend we have navigation containers , that could store all data in any format you choose -- xml-config for example. And then with help of zend view navigation helpers we could output it in our layout or in concrete page.

As an example you could see autotranslated post about it here .

To clarify if I understood your goal correctly: is that you want a single file to handle all your pages' titles and parents?

Of Course: 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]

However you cannot use.html files in your filesystem structure because it is static. The files need to be in.php, but with the configuration of that htaccess, the URL will be remain.html.

The URL about/index.html will look for about/index.php and passes two variables to it: parent and title. The php files will also need to be organized according to your URL directory like index.php , staff.php in /about folder.

In the about/index.php file, you can output:

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

Research more on htaccess if my answer is insufficient, but I think this is what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM