Ad

Custome Middleware Using It In Blade

How can I use this custome middleware HouseOwnerMiddleware. My thought is that if I can use @guest in blade, why not use @houesowner as well?

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        'houseowner' => \App\Http\Middleware\HouseOwnerMiddleware::class, // THIS PART
    ];

But I can not do this. How would you recommend me to use it?

@houseowner
<a class="dropdown-item" target="_blank" rel="nofollow noreferrer" href="/houseowner/roomid/1/userid/{{Auth::id()}}/images/create">
     Upload images
</a>
@end

The custom middleware is here, just for simplification I have made the if statement TRUE. So everyone would pass this statement for now.

class HouseOwnerMiddleware
{
    public function handle($request, Closure $next)
    {
        if(true){
            return $next($request);
        }
    }
}
Ad

Answer

Hmm, i'm not sure what you are trying to do here.


Middleware

A Middleware is used to apply logic in the middle of a request (e.g. checking authorization, adding redirects, modifying the request). The middleware you have written will run but do nothing to your request!

if (true) {
    return $next($request);
}

The if condition will always pass but all it does is say "move onto the next part of the request"

Documentation on Middleware can be found here


Blade Directives

If you want to extend Blade and register new directives like @houseowner, you would do this in a ServiceProvider such as AppServiceProvider like so:

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Blade::directive('houseowner', function ($expression) {
        // Return your custom markup here
        return "<?php echo "houseowner"; ?>";
    });
}

Documentation on extending Blade templates can be found here


Blade Logic

What i suspect you are trying to do is some conditional logic to display an a tag if the user is a "houseowner"?

You can do this with standard blade logic statements like so:

@if (Auth::user->isHouseOwner())
<a class="dropdown-item" target="_blank" rel="nofollow noreferrer" href="/houseowner/roomid/1/userid/{{Auth::id()}}/images/create">
     Upload images
</a>
@endif

You will need to create a method called isHouseOwner() on your User model of course.


It sounds like you are fairly new to Laravel, I would strongly suggest spending some time reading through the documentation. Good Luck!

Ad
source: stackoverflow.com
Ad