Skip to content

Commit ea720f2

Browse files
committed
Merge pull request Zizaco#399 from vpratfr/feature/blade
Blade support @vpratfr
2 parents 3c56c8e + effa46e commit ea720f2

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ contains the latest entrust version for Laravel 4.
2727
- [Concepts](#concepts)
2828
- [Checking for Roles & Permissions](#checking-for-roles--permissions)
2929
- [User ability](#user-ability)
30+
- [Blade templates](#blade-templates)
3031
- [Middleware](#middleware)
3132
- [Short syntax route filter](#short-syntax-route-filter)
3233
- [Route filter](#route-filter)
@@ -355,6 +356,28 @@ Entrust::ability('admin,owner', 'create-post,edit-user');
355356
Auth::user()->ability('admin,owner', 'create-post,edit-user');
356357
```
357358

359+
### Blade templates
360+
361+
Three directives are available for use within your Blade templates. What you give as the directive arguments will be directly passed to the corresponding `Entrust` function.
362+
363+
```php
364+
@role('admin')
365+
<p>This is visible to users with the admin role. Gets translated to
366+
\Entrust::role('admin')</p>
367+
@endrole
368+
369+
@permission('manage-admins')
370+
<p>This is visible to users with the given permissions. Gets translated to
371+
\Entrust::can('manage-admins'). The @can directive is already taken by core
372+
laravel authorization package, hence the @permission directive instead.</p>
373+
@endpermission
374+
375+
@ability('admin,owner', 'create-post,edit-user')
376+
<p>This is visible to users with the given abilities. Gets translated to
377+
\Entrust::ability('admin,owner', 'create-post,edit-user')</p>
378+
@endability
379+
```
380+
358381
### Middleware
359382

360383
You can use a middleware to filter routes and route groups by permission or role

src/Entrust/EntrustServiceProvider.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function boot()
3333

3434
// Register commands
3535
$this->commands('command.entrust.migration');
36+
37+
// Register blade directives
38+
$this->bladeDirectives();
3639
}
3740

3841
/**
@@ -49,6 +52,41 @@ public function register()
4952
$this->mergeConfig();
5053
}
5154

55+
/**
56+
* Register the blade directives
57+
*
58+
* @return void
59+
*/
60+
private function bladeDirectives()
61+
{
62+
// Call to Entrust::hasRole
63+
Blade::directive('role', function($expression) {
64+
return "<?php if (\\Entrust::hasRole{$expression}) : ?>";
65+
});
66+
67+
Blade::directive('endrole', function($expression) {
68+
return "<?php endif; // Entrust::hasRole ?>";
69+
});
70+
71+
// Call to Entrust::can
72+
Blade::directive('permission', function($expression) {
73+
return "<?php if (\\Entrust::can{$expression}) : ?>";
74+
});
75+
76+
Blade::directive('endpermission', function($expression) {
77+
return "<?php endif; // Entrust::can ?>";
78+
});
79+
80+
// Call to Entrust::ability
81+
Blade::directive('ability', function($expression) {
82+
return "<?php if (\\Entrust::ability{$expression}) : ?>";
83+
});
84+
85+
Blade::directive('endability', function($expression) {
86+
return "<?php endif; // Entrust::ability ?>";
87+
});
88+
}
89+
5290
/**
5391
* Register the application bindings.
5492
*

0 commit comments

Comments
 (0)