Laravel:如何在刀片模板中检查中间件

Laravel:如何在刀片模板中检查中间件,laravel,laravel-5,laravel-5.3,laravel-middleware,Laravel,Laravel 5,Laravel 5.3,Laravel Middleware,我已经创建了中间件 php artisan make:middleware isTeacher 在App/Http/isTeacher.php中,我设置了检查: namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Auth; class isTeacher { public function handle($request, Closure $next) { $user = A

我已经创建了中间件

php artisan make:middleware isTeacher
在App/Http/isTeacher.php中,我设置了检查:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class isTeacher
{
  public function handle($request, Closure $next)
  {
    $user = Auth::user();

    if($user && $user->capability == 3)
    {
        return $next($request);
    }
    else
        return redirect('/login');
  }
}
然后,我在app/Http/Kernel.php中定义了中间件

protected $routeMiddleware = [
    ...
    'auth.teacher' => \App\Http\Middleware\isTeacher::class,
    ...
];
问题是:我如何检查刀片模板中的教师能力? 我正在尝试这样做:

@if (Auth::isTeacher())
但是不起作用


非常感谢您提供的任何帮助。

在laravel应用程序中,中间件用于过滤HTTP请求或其外层洋葱。它未定义为在blade中用于决定应呈现html的哪一部分

您可以过滤控制器函数,使其仅在通过中间件(例如,如果经过身份验证)时可用,并且控制器仅在通过中间件时运行该函数

在刀片服务器中使用Auth时,您不是在使用中间件,而是在使用facade,您可以为您的案例执行其使用会话,如:

在您的中间件中

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class isTeacher
{
  public function handle($request, Closure $next)
  {
    $user = Auth::user();

    if($user && $user->capability == 3)
    {
        \session('isTeacher', true);
        return $next($request);
    }

    return redirect('/login');

}
然后在您的刀片中执行以下操作:

@if (Session::get('isTeacher', 0));

这将只向教师显示内容,如果未设置会话,它将返回到0或false。

在这种情况下,使用中间件并不是真正的解决方案。我建议使用blade指令,它允许您创建自定义blade功能,如:

@author
    //Do something
@else
    //Something else
@endauthor
要使用上述语法,可以在AppServiceProvider.php文件中注册新的blade指令

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('author', function () {
            $isAuth = false;

            // check if the user authenticated is teacher
            if (auth()->user() && auth()->user()->capability == 3) {

                $isAuth = true;
            }

            return "<?php if ($isAuth) { ?>";
        });

        Blade::directive('endauthor', function () {
            return "<?php } ?>";
        });
    }
}
在AppServiceProvider.php文件中进行上述更改后,运行php artisan view:clear


为了更好地理解,您可以参考文档

这是莱昂内尔王子答案的改进版本。请注意Blade::directive回调函数的返回值

@author
    //Do something
@else
    //Something else
@endauthor
AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('author', function () {
            $isAuth = false;

            // check if the user authenticated is teacher
            if (auth()->user() && auth()->user()->capability == 3) {

                $isAuth = true;
            }

            return "<?php if (" . intval($isAuth) . ") { ?>";
        });

        Blade::directive('endauthor', function () {
            return "<?php } ?>";
        });
    }
}

blade指令只需要返回true或false,其他什么都不需要

您可以在AppServiceProvider.php的引导方法中执行以下操作

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Blade::directive('author', function () {
            $isAuth = false;

            // check if the user authenticated is teacher
            if (auth()->user() && auth()->user()->capability == 3) {

                $isAuth = true;
            }

            return "<?php if (" . intval($isAuth) . ") { ?>";
        });

        Blade::directive('endauthor', function () {
            return "<?php } ?>";
        });
    }
}
刀片::如果是'teacher',函数{ 返回auth->user&&$user->capability==3;
};您已经收到的答案是正确的,因为中间件不适合您的用例,但是,您不应该使用会话,而应该考虑使用策略来实现这一点,以下是文档:请您给出这种方法看起来不太好的原因?谢谢,但这段代码不起作用。刀片服务器在if条件下返回错误。当$isAuth==false时,此答案将不起作用。因为Boolean valuefalse将被转换为空字符串,所以您的blade中会有这样的代码,这显然会产生错误谢谢,这段代码工作正常。我的疑问是:当用户注销时,会话isTeacher仍然存在?注销会话将刷新它!你为什么删除我的答案@马里奥布罗斯