Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Automatically determine breadcrumbs from current route #16

Closed
d13r opened this issue Sep 1, 2013 · 4 comments
Closed

Automatically determine breadcrumbs from current route #16

d13r opened this issue Sep 1, 2013 · 4 comments
Assignees

Comments

@d13r
Copy link
Owner

d13r commented Sep 1, 2013

I don't yet know if this is possible but I'd like to try to determine the current breadcrumbs from the current route name & parameters, so that it's not necessary to specify these again in the view.

This may not be possible since not all routes are named...

@imevul
Copy link

imevul commented Nov 14, 2013

I use this code, which works but probably isn't the best solution.

function has_breadcrumb($name = NULL) {
    if ($name === NULL) {
        $name = Route::currentRouteName();
    }

    $excluded = ['home']; //TODO Move to config.
    if (in_array($name, $excluded)) {
        return FALSE;
    }

    return in_array($name, Breadcrumbs::getRegistered());
}

/**
 * Render the breadcrumb for a certain route. (If it has one.)
 * @param string $name The route to display breadcrumbs for.
 * @param array $routeParameters Any route parameters to use. For example, the selected category.
 * @return string|NULL Returns the breadcrumb HTML if successful, otherwise NULL.
 */
function render_breadcrumb($name = NULL, $routeParameters = array()) {
    if ($name === NULL) {
        $name = Route::currentRouteName();
    }

    if (has_breadcrumb($name)) {
        return Breadcrumbs::render($name, $routeParameters);
    }
}

As you can see, it requires a small modification to the Breadcrumbs library; a way to get the currently added breadcrumbs. ($this->callbacks)

This only works for named routes though, and you have to send func_get_args() as $routeParameters to the view, which can be a bit of a hassle. The best thing would be if you could just call Breadcrumbs::render() without any parameters and it would get the current route stuff automatically.

Edit: This doesn't actually work since the 2.x update.

@d13r
Copy link
Owner Author

d13r commented Nov 16, 2013

Since 2.0 you probably need Breadcrumbs::renderArray($name, $routeParameters) instead.

An alternative to has_breadcrumb() would be to put a try/catch around the call to render(), but I'll keep that in mind and perhaps add Breadcrumbs::exists() or similar too.

@Andreyco
Copy link
Contributor

I think this is resolved here #24

@d13r d13r closed this as completed Jan 26, 2014
@d13r
Copy link
Owner Author

d13r commented Jan 26, 2014

Released in 2.2.0.

@k1ng440 k1ng440 mentioned this issue Dec 14, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants