Support the ongoing development of Laravel.io →
posted 10 years ago
Views Blade
Last updated 2 years ago.
0

If you're trying to call a controller function from anywhere other than a route, it's usually a sign that you're doing something wrong. Controllers are supposed to just be a transport layer between a HTTP request and your application, after all. And your views should be as "dumb" as possible - no logic other than pure presentation logic, like iterating over an array to build an HTML table. I can think of at least two ways to handle that:

1 - Use a View Composer - specify what section of the nav you're loading in the controller and then use the view composer to add the correct navigation, like so:

class NavigationViewComposer
{

    protected $sections = array(
        'auctions' => array(
            '/auctions/latest' => 'Latest Auctions',
            //etc
        ),
        'users' => array(
            '/users/you' => 'Your Profile',
            //etc
        ),
        //etc
        
    )

    public function compose($view)
    {
        $data = $view->getData();
        $section = isset($data['section']) ? $data['section'] : 'site';
        $view->with('navigation', $this->sections[$section]);
    }

}

2 - Use partials - Have a different layout (that extends your master layout) for each section, then put the section-specific navigation there. For example:

//file: layouts/auctions.blade.php
@extends('layouts/master')

@section('nav')
<li><a href="/auctions/latest">Latest Auctions</a></li>
<!-- etc -->
@stop

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

pasternt pasternt Joined 7 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.