Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blade directives not working #278

Closed
BartHuis opened this issue Apr 26, 2017 · 8 comments
Closed

blade directives not working #278

BartHuis opened this issue Apr 26, 2017 · 8 comments

Comments

@BartHuis
Copy link

Hi,
After the tweet yesterday, here are my debug actions i tried:
vendor\spatie\laravel-permission\src\PermissionServiceProvider.php:

protected function registerBladeExtensions()
    {
        dd("test");
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
            $bladeCompiler->directive('role', function ($arguments) {
                list($role, $guard) = explode(',', $arguments.',');

                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
            });
......................

gives test when loading my page

vendor\spatie\laravel-permission\src\PermissionServiceProvider.php:

protected function registerBladeExtensions()
    {
        $this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
            dd("test");
            $bladeCompiler->directive('role', function ($arguments) {
                list($role, $guard) = explode(',', $arguments.',');

                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
            });
......................

does just give
@ROLE('admin')
admin only
@Endrole

as output from the blade

@BartHuis
Copy link
Author

i see the service provider of entrust calls the blade directive different, and had everything in the boot instead of registering it:
https://github.com/Zizaco/entrust/blob/master/src/Entrust/EntrustServiceProvider.php

i don't know enough about boot/register and everything to understand what's happening here, but when i change the way of calling /Blade, it's not working, when i put everything from register to boot, it doesn't work, but if i do both, it works.

so when i change your service provider to:

<?php

namespace Spatie\Permission;

use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\Contracts\Permission as PermissionContract;

class PermissionServiceProvider extends ServiceProvider
{
    public function boot(PermissionRegistrar $permissionLoader)
    {
        $this->publishes([
            __DIR__.'/../config/permission.php' => $this->app->configPath().'/permission.php',
        ], 'config');

        if (! class_exists('CreatePermissionTables')) {
            $timestamp = date('Y_m_d_His', time());

            $this->publishes([
                __DIR__.'/../database/migrations/create_permission_tables.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_permission_tables.php",
            ], 'migrations');
        }

        $this->registerModelBindings();

        $permissionLoader->registerPermissions();
    
        $this->mergeConfigFrom(
            __DIR__.'/../config/permission.php',
            'permission'
        );
        $this->registerBladeExtensions();
    }

    public function register()
    {
    }

    protected function registerModelBindings()
    {
        $config = $this->app->config['permission.models'];

        $this->app->bind(PermissionContract::class, $config['permission']);
        $this->app->bind(RoleContract::class, $config['role']);
    }

    protected function registerBladeExtensions()
    {
        
        \Blade::directive('role', function ($arguments) {
            list($role, $guard) = explode(',', $arguments.',');

            return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
        });
        
        \Blade::directive('endrole', function ($arguments) {
            return '<?php endif; ?>';
        });
    
        \Blade::directive('hasrole', function ($arguments) {
                list($role, $guard) = explode(',', $arguments.',');

                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
            });
        \Blade::directive('endhasrole', function () {
                return '<?php endif; ?>';
            });
    
        \Blade::directive('hasanyrole', function ($arguments) {
                list($roles, $guard) = explode(',', $arguments.',');

                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAnyRole({$roles})): ?>";
            });
        \Blade::directive('endhasanyrole', function () {
                return '<?php endif; ?>';
            });
    
        \Blade::directive('hasallroles', function ($arguments) {
                list($roles, $guard) = explode(',', $arguments.',');

                return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasAllRoles({$roles})): ?>";
            });
        \Blade::directive('endhasallroles', function () {
                return '<?php endif; ?>';
            });
    }
}

the role directive works. is there something wrong with my setup why you original doesn't work for me? of is there a change in laravel why this change is needed?

i see everyone at entrust having problems with the blade directives are advised to replace their serviceprovider, so maybe there's a change in laravel or something?
Zizaco/entrust#418

@freekmurze
Copy link
Member

The tests seem to point out that the current service provider + directives are working.

I suspect something app specific is causing troubles. Could you please try installing the package in a vanilla Laravel app?

@freekmurze
Copy link
Member

Closing this for now, reopen if this fails for you in a vanilla app.

@BartHuis
Copy link
Author

Zal morgen ff kijken, koningsdag gevierd vandaag ;)

@BartHuis
Copy link
Author

Good morning @freekmurze,
i'll try the vanilla app later, as this temp fix works for us, first some prio's to do.

@edwinheij
Copy link

I am moving from Entrust to this one, but also won't execute the code within 'afterRosolving':

$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
....

Have you fixed this issue @BartHuis ?
ps: in a vanilla Laravel project the BladeDirectives are working BTW!

@edwinheij
Copy link

@BartHuis
Copy link
Author

BartHuis commented Jun 9, 2017

hmm, i don't have any directions yet in my app service provider, so don't think that's the problem here, but we just moved to santigarcor/laratrust, no problems anymore...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants