1

I'm having a bit of an issue with a breadcrumb i'm making for a site... I'm relatively new to Kohana so i'm not sure if the way i'm going about this is correct.

I have all my pages with their correct controllers working fine, i needed a breadcrumb at the top of each one, which due to the design had to go at the top of each view - this was getting included using
<?=View::factory('elements/breadcrumb')?>

this however only include the actual view and i want to grab it's controller (and model via the controller) because i need to work out which page were on and change the breadcrumb view accordingly.

I'm been poking around and come across HMVC but i'm not really sure how this works or if it's even what i should be looking at!

Can anyone shed some light on this??

P.S. rather than just using a module someone else has already made, i'd quite like to understand how the hell to do this properly - i'm close, i can feel it!

Richard
  • 11
  • 2

2 Answers2

1

Why don't you build the breadcrumbs array in your controller and pass that data into the view?

<?=View::factory('elements/breadcrumb', $breadcrumbs)?>
badsyntax
  • 9,394
  • 3
  • 49
  • 67
0

I've been doing some thinking on breadcrumbs recently and have come up with the following - my goal was to avoid repetition by associating the breadcrumb component with the route. So if you have the url:

/blog/my-article

Then it would first find the breadcrumb title for that URL, and then:

/blog

I've set up my routes like so:

Route::set('blog', 'blog')
    ->defaults(array(
        'controller' => 'blog',
        'action'     => 'index',
        'breadcrumb' => 'Blog'
    )
);

Route::set('module_view', 'blog/<slug>')
    ->defaults(array(
        'controller' => 'module',
        'action'     => 'view',
        'breadcrumb' => function($params=null) {
            return ORM::factory('article')->where('slug', '=', $params['slug'])->find()->name;
        }
    )
);

Note, breadcrumb can be either fixed text/value, or a callback allowing it to be dynamically generated.

Then in my base controller, I have this:

protected function breadcrumb() {

    $breadcrumb = array();

    $uri = substr(Request::detect_uri(), 1);
    $routes = Route::all();
    while ($uri) { 
        foreach($routes as $name => $route) {
            if ($params = $route->matches($uri)) {
                /* @var $route Route */
                if (isset($params['breadcrumb'])) {
                    if (is_callable($params['breadcrumb'])) {
                        $name = $params['breadcrumb']($params);
                    } else {
                        $name = $params['breadcrumb'];
                    }
                    $breadcrumb[$name] =  URL::site($route->uri($params));

                };
                break;
            }
        }
        $uri = preg_replace('#(^|/)[^/]*/*?$#', '', $uri);
    }

    return array_reverse($breadcrumb);

}

Warning: some may consider this pretty messy, but it's working nicely for me.

badsyntax
  • 9,394
  • 3
  • 49
  • 67
Colin
  • 784
  • 2
  • 8
  • 21