关于如何在 CMS 中为动态内容构建面包屑路径的建议

发布于 2024-11-26 09:23:02 字数 1025 浏览 0 评论 0原文

我对我们构建的 CMS 的各个部分有一个 URL 命名约定,并且希望在您浏览它时构建一条面包屑路径。我遇到的问题是你不能简单地用反向链接(和大写单词)替换 URL 路径的部分; URL 路径的每个部分都告诉 CMS 要执行哪些操作。我将尽力在此空间中进行说明:

URL:

/news/list/item/1

将创建面包屑:

Home > News > General News (category ID=1)

并列出 categoryID=1 所在的新闻帖子。

但同时 URL:

/news/add/category

将创建面包屑:

Home > News > Add Category

并且 URL:

/news/edit/item/25 (let's assume post 25 is in category 1)

将创建面包屑:

Home > News > General News > Edit Post (post ID=25)

并显示用于编辑的 postID=25

因此,您希望能够看到我在哪里被绊倒; “列表”、“编辑”和“添加”基本上是您在 CMS 中移动时可以执行的三件事(删除的操作略有不同,以防有人好奇我为什么不这样做),但面包屑不匹配逐级的 URL。因此,这并不是简单地将 URL 路径解析为数组并根据各个部分创建面包屑的问题。

我希望我能够很好地解释这一点......这与其说是一个编码问题(尽管任何示例代码都会受到赞赏),不如说是一个关于如何解决这个问题的结构和策略的问题。另外,值得注意的是,URL 的“新闻”部分将更改为安装在 CMS 中的其他模块,每个模块都有一个单独的 Module.class.php 文件,其中存储了所有功能。

I have a URL naming convention for various parts of a CMS we built, and want to build a breadcrumb trail as you move through it. The problem I'm having is that you can't simply replace the parts of the URL path with back links (and capitalized words); each part of the URL path tells the CMS what actions to perform. I'm going to do my best to illustrate in this space:

The URL:

/news/list/item/1

would create the breadcrumb:

Home > News > General News (category ID=1)

and list the news posts where categoryID=1.

But at the same time the URL:

/news/add/category

would create the breadcrumb:

Home > News > Add Category

And the URL:

/news/edit/item/25 (let's assume post 25 is in category 1)

would create the breadcrumb:

Home > News > General News > Edit Post (post ID=25)

and display the postID=25 for editing

So you hopefully can see where I'm getting tripped up; "list" "edit" and "add" are basically the three things you can do as you're moving through the CMS (delete's done a little differently in case anyone's curious why I left that off), but the breadcrumbs don't match the URL level-for-level. Thus, it isn't a matter of simply parsing the URL path into an array and creating the breadcrumb based on the various parts.

I hope I was able to explain this well enough...this isn't so much a coding question (although any sample code would be appreciated), rather a question about structure and strategy on how to approach this. Also, it's worth noting that the "news" part of the URL will change to other modules that are installed in the CMS, each having a separate Module.class.php file where all their functions are stored.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-12-03 09:23:02

您没有分享太多有关 CMS 设计的信息,例如如何将请求构建为模块和操作。然而,只要您需要面包屑,这些通常只是链接列表。

如果将列表(数组)封装到类中,则可以使用请求实例化面包屑,然后使应用程序的各个部分添加它们需要添加到其中的面包屑。

前端控制器可以添加主页链接(Home)。该模块可以为索引操作(新闻)添加模块链接,然后特定操作可以添加适合的最后链接(一般新闻)。

他们都只是共享面包屑对象以及请求和完成的工作。

这是针对 MVC 类型的应用程序,我认为它很容易适用于任何类型的设计。

class BreadcrumbTrail
{
    private $breadcrumbs = array();
    public function add($label, $link)
    {
        $this->breadcrumbs[] = array($label, $link);
    }
    public function toArray()
    {
        return $this->breadcrumbs;
    }
}


$breadcrumbs = new BreadcrumbTrail();
$breadcrumbs->add('Home', '/');

然后将面包屑作为依赖项注入,或使用某些上下文将其提供给您拥有的不同模块/控制器。

您的应用程序增长得越多或您需要的功能越多,您就可以扩展此结构(例如接口、面包屑本身的具体类等),这只是为了演示基本思想。

You have not shared much about the design of your CMS, e.g. how you structure the requests into modules and actions. However as long as a breadcrumb is what you ask for, those normally are just a list of links.

If you encapsulate a list (array) into a class, you can instantiate the breadcrumb with the request and than make various parts of your application add the breadcrumbs they need to add to it.

The frontend controller could add the home link (Home). The module can add the module link for the index action (News), the specific action can then add the last links as it suits (General News).

They all just share the breadcrumb object with the request and job done.

That's for a MVC type application, I think it's easily to adopt for any sort of design.

class BreadcrumbTrail
{
    private $breadcrumbs = array();
    public function add($label, $link)
    {
        $this->breadcrumbs[] = array($label, $link);
    }
    public function toArray()
    {
        return $this->breadcrumbs;
    }
}


$breadcrumbs = new BreadcrumbTrail();
$breadcrumbs->add('Home', '/');

Inject the breadcrumb then as a dependency or use some context to provide it to the different modules / controllers you have.

The more your application grows or the more features you need, you can extend this structure (e.g interfaces, a concrete class for the breadcrumb itself etc.), this is just for demonstrating the basic idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文