Kenny
Kenny

Reputation: 2176

Proper way to include subviews conditionally in Laravel

In Laravel 4, is there a best practice for including subviews conditionally?

My main menu page should include different snippets depending upon the permissions the user has.

Here is what I currently have started. I put an 'if' statement in the view:

@extends("layout")
@section("content")
<h2>This is the Main Menu</h2>


@if(Auth::user()->permissions & Config::get('permissions.aw'))

    @include('awMenu')

@endif
@stop

I can see that this may get ugly, since there can be up to 31 different permissions. (No, I won't be using that many.) Can someone recommend a better way for me to approach this?

Upvotes: 1

Views: 437

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24661

Here's one idea...

In your main view:

@include('permission-views')

In permission-views:

@include('awMenu')
@include('myOtherPermissionView')
...

In awMenu and all of your other permission views:

@if(Auth::user()->permissions & Config::get('permissions.aw'))

    ... content to show if the user has permission ...

@endif

This will at least keep your main view from becoming too cluttered and push the responsibility for determining access down into the view responsible for displaying the content.

Upvotes: 2

Related Questions