Suggestions on how to build a breadcrumb trail for dynamic content in a CMS
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.
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.
精彩评论