What is the most elegant way to print breadcrumbs on a website? -


i've been thinking long time already. best, or elegant, way of creating breadcrumbs?

one way have method called controllers. this:

function showusers() {     $this->breadcrumbs->add('users', url::route('admin.users'));     // ... } 

i don't way, though, because i'd separate breadcrumb-logic controllers. also, if i'd e.g. have admin panel -item many pages inside controller, i'd need either define in constructor or in every controller method.

another way utilize named routes, breaking them segments. requires routes sensibly named , structured in way. here's pseudocode:

if($segment[0] == 'admin') {     $breadcrumbs->add('admin panel', url::route('admin');     if($segment[1] == 'users') {         $breadcrumbs->add('users', url::route('admin.users');     } elseif($segment[1] == 'foo') {         $breadcrumbs->add(...);     } } 

one issue approach it's hard "data" regarding current route. example, can have route showing single user (admin.users.single), gets user id in route (e.g. admin/users/{id}). there no native way me construct correct url breadcrumb item, route data used inside controller.

can think of other ways? opinions on these examples?

to make more generic instead of having conditional statements on users , user names etc. in controller. think best first define kind of hierarchy or tree of categories. storing info on backend (i.e. db, text file, nosql):

+--------------+--------------+---------------------+------------+ |  categoryid  | categoryname | categorydisplayname | parentid   | +--------------+--------------+---------------------+------------+ |      1       | admin        |  admin panel        | null       | +--------------+--------------+---------------------+------------+ |      2       | admin.users  |  users              | 1          | +--------------+--------------+---------------------+------------+ 

you can separate out logic of traversing categories , building of breadcrumb string. easier understand, test, etc.

i.e. c#

you can retrieve list of breadcrumbs based on data:

ienumerable<category> gethierarchy(string categoryname) {   ... } 

i.e. gethierarchy("users") return list "admin panel", "users"

then build breadcrumb string:

var currentpage = "admin.users" strings.join(">", gethierarchy(currentpage)                    .select(c => getlink(c.categoryname, c.categorydisplayname))) 

you can embed view helpers or set controller.

getlink method return link containing route category. i.e.

<a href="admin/users">users</a>


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -