entrust icon indicating copy to clipboard operation
entrust copied to clipboard

Blade directives not working!

Open introwit opened this issue 11 years ago • 30 comments

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

introwit avatar Nov 20 '15 10:11 introwit

+1

Have reverted to using

@if(Entrust::hasRole('admin')) @endif

tomhoad avatar Nov 20 '15 18:11 tomhoad

@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

introwit avatar Nov 23 '15 08:11 introwit

I am also facing this issue. I tried @harishtoshniwal solution. but, no luck. anyone got solutions?

shanka12 avatar Nov 24 '15 06:11 shanka12

@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 avatar Nov 24 '15 07:11 shanka12

@shanka12 what version of laravel are you using?

introwit avatar Nov 24 '15 08:11 introwit

@harishtoshniwal laravel 5

shanka12 avatar Nov 24 '15 08:11 shanka12

@shanka12 be more specific with the laravel version number please.

introwit avatar Nov 24 '15 09:11 introwit

@harishtoshniwal laravel 5.0.

shanka12 avatar Nov 24 '15 09:11 shanka12

I also have this issue, using Laravel 5.1 and Entrust dev-laravel-5.

@tomhoad thanks! that solution works perfectly

thomasm0 avatar Nov 25 '15 09:11 thomasm0

@tomhoad how to use ability method on view file?

shanka12 avatar Nov 25 '15 17:11 shanka12

+1

AmanaDEV avatar Dec 04 '15 14:12 AmanaDEV

Change documentation and remove blade directives!

AlexRadch avatar Dec 09 '15 10:12 AlexRadch

I'm having this issue as well, same as @thomasbandit

BojanKogoj avatar Dec 16 '15 10:12 BojanKogoj

si funciona...!! tenemos q copiar todo el contenido de el ServiceProvider.php del Repositorio a nuestro ServiceProvider.php de nuestra proyecto

michini avatar Jan 12 '16 04:01 michini

I've this issue with laravel 5.1

mortezarajabi avatar Jan 23 '16 10:01 mortezarajabi

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,
...
],

GregPeden avatar Jan 28 '16 23:01 GregPeden

@SirLamer Thank you for this - blade directives working a treat now in 5.2

sunscreem avatar Jan 31 '16 18:01 sunscreem

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.

GregPeden avatar Feb 01 '16 19:02 GregPeden

Try using "zizaco/entrust": "^1.5" instead of "zizaco/entrust": "dev-laravel-5", which has the blade directives.

mseymour avatar Feb 02 '16 14:02 mseymour

@mseymour I tried what you said but it still doesn't work. Has it worked for anybody else?

diegofal avatar Feb 10 '16 17:02 diegofal