Blade directives not working!
All the 3 blade directives are not working on my blade views in the laravel app. Rather they show up as plain text. Example below:
@role('admin')
Something here
@endrole
outputs this on my view in the browser: @role('admin') Something here @endrole
+1
Have reverted to using
@if(Entrust::hasRole('admin')) @endif
@tomhoad We managed to solve the problem so sharing the solution with you.
- The current branch which is mentioned for installation in the docs does not contain the logic for the blade directives.
- We have found the the logic for the blade directives in the main master branch.
- So simply replace your file in the Vendors/Zizaco/entrust/src/Entrust/EntrustServiceProvider.php with the file below: https://github.com/Zizaco/entrust/blob/master/src/Entrust/EntrustServiceProvider.php
I am also facing this issue. I tried @harishtoshniwal solution. but, no luck. anyone got solutions?
@harishtoshniwal when I use your solution, getting following error.
FatalErrorException in Facade.php line 213: Call to undefined method Illuminate\View\Compilers\BladeCompiler::directive()
@shanka12 what version of laravel are you using?
@harishtoshniwal laravel 5
@shanka12 be more specific with the laravel version number please.
@harishtoshniwal laravel 5.0.
I also have this issue, using Laravel 5.1 and Entrust dev-laravel-5.
@tomhoad thanks! that solution works perfectly
@tomhoad how to use ability method on view file?
+1
Change documentation and remove blade directives!
I'm having this issue as well, same as @thomasbandit
si funciona...!! tenemos q copiar todo el contenido de el ServiceProvider.php del Repositorio a nuestro ServiceProvider.php de nuestra proyecto
I've this issue with laravel 5.1
Just to add to this, I would advise against editing the script files in the vendor package, since they will be overwritten during future package updates, if any. Also, any package distributions of your own work won't include your changes to the Entrust code. You must always treat the content inside of the vendor folder as off-limits to your keyboard.
Instead, extend the EntrustServiceProvider in your own workspace and then direct the provider reference in app.cfg to that.
For now, I am going with this route even though there is a 5.2 development branch available since the original post because that branch includes some PHP line comments which breaks one of the driver dependencies in Laravel.
In this case I chose to change the name of the service provider so that it is clear which is which for IDE auto-completion, but you could also name the class the same and be careful with namespaces if you want.
App\Providers\EntrustCustomServiceProvider.php:
namespace App\Providers;
class EntrustCustomServiceProvider extends \Zizaco\Entrust\EntrustServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
parent::boot();
// Register blade directives
$this->bladeDirectives();
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
parent::register();
}
/**
* Register the blade directives
*
* @return void
*/
private function bladeDirectives()
{
// Call to Entrust::hasRole
\Blade::directive('role', function($expression) {
return "<?php if (\\Entrust::hasRole{$expression}) : ?>";
});
\Blade::directive('endrole', function($expression) {
return "<?php endif; // Entrust::hasRole ?>";
});
// Call to Entrust::can
\Blade::directive('permission', function($expression) {
return "<?php if (\\Entrust::can{$expression}) : ?>";
});
\Blade::directive('endpermission', function($expression) {
return "<?php endif; // Entrust::can ?>";
});
// Call to Entrust::ability
\Blade::directive('ability', function($expression) {
return "<?php if (\\Entrust::ability{$expression}) : ?>";
});
\Blade::directive('endability', function($expression) {
return "<?php endif; // Entrust::ability ?>";
});
}
}
In config\app.php, change your service provider reference, being sure to remove the old Entrust provider reference if it is entered already:
'providers' => [
...
\App\Providers\EntrustCustomServiceProvider::class,
...
],
Also, while you're in config/app.php, don't forget there is a typo in the readme regarding the Entrust facade, so make sure the Facade is set as so:
'aliases' => [
...
'Entrust' => \Zizaco\Entrust\EntrustFacade::class,
...
],
@SirLamer Thank you for this - blade directives working a treat now in 5.2
I tried using the 5.2 branch but received many errors about comments in the code not being allowed. I understand the MySQL database driver does not allow code comments in the portions of code it handles - or something like that, don't quote me on the specifics. So I have to assume someone is developing and testing with a filesystem driver for data storage thus this wasn't caught. I chose to just stick to 5.1 in the meantime.
I am looking forward to the 5.2 branch as well, as I understand it also adds data caching so that the database doesn't get hammered with the same query again and again to check for roles - fine for me right now when I am developing myself, but would be bad for production.
Try using "zizaco/entrust": "^1.5" instead of "zizaco/entrust": "dev-laravel-5", which has the blade directives.
@mseymour I tried what you said but it still doesn't work. Has it worked for anybody else?