mercredi 6 avril 2016

Laravel blade @can policy - string

I am using Laravel 5.2. So I'm learning about how to deal with roles and permissions Authorization. Everything runs fine. I even made my own policy PostPolicy.

And now to the problem. I load the $post data into the view in the PostsController which then loads in blade.

PostsController:

public function show($id)
{
    $post = Post::find($id);

    return view('posts.show', compact('post'));
}

posts/show.blade.php:

@section('content')
<!-- begin -->
@can('hasRole', Auth::user())
    <h1>Displaying Admin content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying moderator content</h1>
@endcan

@can('hasRole', Auth::user())
    <h1>Displaying guest content</h1>
@endcan

Policy:

  public function hasRole($user)
    {
        // just for test
        return true;
    }

Now that returns all the content.

When I change the @can('hasRole', Auth::user()) from Auth::user() to a string, i.E.

@can('hasRole', 'guest')
    <h1>Displaying guest content</h1>
@endcan

In this case it doesn't return anything. As I am new to Laravel, I really don't know it doesn't work.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire