Laravel:如何检查刀片模板中的中间件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48477239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: how to check middleware in blade template
提问by mariobros
i've created Middleware
我已经创建了中间件
php artisan make:middleware isTeacher
in App/Http/isTeacher.php i've placed the check:
在 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');
}
}
than, i've defined the middleware in app/Http/Kernel.php
比,我已经在 app/Http/Kernel.php 中定义了中间件
protected $routeMiddleware = [
...
'auth.teacher' => \App\Http\Middleware\isTeacher::class,
...
];
The question is: how I check the teacher capability in blade template? I'm trying with this:
问题是:我如何检查刀片模板中的教师能力?我正在尝试这样做:
@if (Auth::isTeacher())
but doesn't work
但不起作用
any help are appreciated
任何帮助表示赞赏
回答by Ash Baker
The blade directive only needs to return true or false nothing else.
Blade 指令只需要返回 true 或 false 没有别的。
You could just do the following as in your boot()
method of the AppServiceProdiver.php
您可以按照您的boot()
方法执行以下操作AppServiceProdiver.php
Blade::if('teacher', function () {
return auth()->user() && $user->capability == 3;
});
Then you will be able to do the following in blade templates:
然后您将能够在刀片模板中执行以下操作:
@teacher
Only teachers will see this
@endteacher
回答by Leo
Well there is an issue here Middlewares are used to filter HTTP
requests or its outter layer of the onion in laravel app. It is not defined to be used in blade to decide which part of html should be rendered.
嗯,这里有一个问题,中间件用于过滤HTTP
请求或其在 laravel 应用程序中的洋葱外层。它没有被定义为在刀片中使用来决定应该呈现 html 的哪一部分。
You can filter a controller function to only be usable if it passes the middleware (if authenticated for example) and the controller only runs the function if it passes on the middleware.
您可以过滤控制器功能,使其仅在通过中间件(例如,如果经过身份验证)时才可用,并且控制器仅在通过中间件时才运行该功能。
When using Auth
in blades you are not using a middleware but a facade
whan you could do its use Session
for your case like:
Auth
在刀片中使用时,您不是在使用中间件,而是在您的情况下facade
使用它Session
,例如:
In your middleware
在你的中间件中
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');
}
Then in your blades do like:
然后在您的刀片中执行以下操作:
@if (Session::get('isTeacher', 0));
This will show content only to teachers, if session its not set it will fall back to 0 or false.
这将仅向教师显示内容,如果会话未设置,它将回退到 0 或 false。
回答by Prince Lionel N'zi
In this scenario, using middleware is not really the solution. I will rather suggest blade directive, which allows you to create a custom blade function like:
在这种情况下,使用中间件并不是真正的解决方案。我宁愿建议刀片指令,它允许您创建自定义刀片功能,例如:
@author
//Do something
@else
//Something else
@endauthor
To use the above syntax, you can register a new blade directive in your AppServiceProvider.php file
要使用上述语法,您可以在 AppServiceProvider.php 文件中注册一个新的刀片指令
<?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 } ?>";
});
}
}
After the above changes in the AppServiceProvider.php
file, run php artisan view:clear
在AppServiceProvider.php
文件中进行上述更改后,运行php artisan view:clear
For a better understanding, you can refer to the documentation here
为了更好地理解,您可以参考此处的文档
回答by Aram Hovhannisyan
This is an improved version of Prince Lionel's answer. Pay attention to the return value of Blade::directive callback function.
这是莱昂内尔王子答案的改进版本。注意 Blade::directive 回调函数的返回值。
@author
//Do something
@else
//Something else
@endauthor
AppServiceProvider.php
应用服务提供者.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 } ?>";
});
}
}