Skip to content

Add blade directive @guest#20114

Merged
taylorotwell merged 4 commits into
laravel:5.4from
LKaemmerling:Add-Blade-Directive-guest
Jul 18, 2017
Merged

Add blade directive @guest#20114
taylorotwell merged 4 commits into
laravel:5.4from
LKaemmerling:Add-Blade-Directive-guest

Conversation

@LKaemmerling

Copy link
Copy Markdown
Contributor

As addition to #20087 it would be nice if there is the opposite of @auth, so i implemented it.

@it-can

it-can commented Jul 18, 2017

Copy link
Copy Markdown
Contributor

If do not pass a guard, will this work?

@m1guelpf

Copy link
Copy Markdown
Contributor

@it-can Yes

@LKaemmerling

Copy link
Copy Markdown
Contributor Author

@it-can yeah it will, it is the same code as #20087. @m1guelpf what do you thing if the @guest can be an "else" for your @auth ?

Like:

@auth

//Is authentificated

@guest 


// Is guest 

@endauth

@m1guelpf

m1guelpf commented Jul 18, 2017

Copy link
Copy Markdown
Contributor

@LKDevelopment That'd be confusing. I'd suggest keeping both PR's as-is, as making the @guest directive work as an else would be confusing if you only want to use guest...

@it-can

it-can commented Jul 18, 2017

Copy link
Copy Markdown
Contributor

@LKDevelopment If I add a custom directive to my laravel 5.4 app, I get a error when calling

AppServiceProvider.php

Blade::directive('auth', function ($guard = null) {
    return "<?php if(auth()->guard{$guard}->check()): ?>";
});
        
Blade::directive('endauth', function () {
    return "<?php endif; ?>";
});

This gives an error

@auth
    logged in
@endauth

Undefined property: Illuminate\Auth\AuthManager::$guard

compiles to:

<?php if(auth()->guard->check()): ?>
logged in
<?php endif; ?>

this works...

@auth('web')
    logged in
@endauth

@m1guelpf

Copy link
Copy Markdown
Contributor

@it-can You have a typo in the directive.
Try this:

Blade::directive('auth', function ($guard = null) {
    return "<?php if(auth()->guard({$guard})->check()): ?>";
});
        
Blade::directive('endauth', function () {
    return "<?php endif; ?>";
});

@it-can

it-can commented Jul 18, 2017

Copy link
Copy Markdown
Contributor

@m1guelpf yes that works... but when I check this PR, the code is missing the parenthesis?

*/
protected function compileGuest($guard = null)
{
return "<?php if(auth()->guard{$guard}->guest()): ?>";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this also be changed in #20087 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems taylor changed it...

a11b44a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No guys!
This is not the guard which is passed, but the complete $expression, with the ().
Nothing to change here!

@m1guelpf m1guelpf Jul 18, 2017

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mathieutu is rigth. There's nothing to change here.

{
return '<?php endif; ?>';
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the extra whitespace here, and your PR is perfect!

@taylorotwell
taylorotwell merged commit c41fb3a into laravel:5.4 Jul 18, 2017
@m1guelpf
m1guelpf deleted the Add-Blade-Directive-guest branch July 18, 2017 13:14
@carusogabriel

carusogabriel commented Aug 10, 2017

Copy link
Copy Markdown
Contributor

@m1guelpf @LKDevelopment Any ideas to work in @LKDevelopment's proposal?

@auth
    // Is authenticated
@guest 
    // Is guest 
@endauth

@m1guelpf

Copy link
Copy Markdown
Contributor

@Gabriel-Caruso I don't think so. Now it'd be a breaking change...

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

Successfully merging this pull request may close these issues.

6 participants