php - 用于订阅的 Laravel 中间件

标签 php laravel-5 laravel-middleware

我正在尝试在 Laravel 5 中编写中间件来检查用户是否有订阅,并且订阅没有被取消。如果它不存在或被取消,那么他们将被锁定在一个页面(计费)中。问题是,我最终陷入了重定向循环。我明白为什么,但我就是想不通正确的做法是什么。提前致谢

public function handle($request, Closure $next)
{


    if (\Auth::check()) 
    {
        // if subscription does not exist
        if (\Auth::user()->hospital->subscription == null || \Auth::user()->hospital->subscription !== 'Active') {
           return redirect('billing');
        }

    }
    return $next($request);
}

最佳答案

Problem is, I end up in a redirect loop.

看起来您正在整个应用程序(包括计费页面)中应用中间件。因此,您需要指定应在何处考虑中间件类,这可以在 /app/Http/kernel.php 中实现。

此外,您可以考虑在您的中间件类中进行额外的验证,例如:

// billing (http://example.com/billing)
$path = $request->path();

if ($path !== 'billing')
{

    if (\Auth::check()) 
    {
        // if subscription does not exist
        if (\Auth::user()->hospital->subscription == null || \Auth::user()->hospital->subscription !== 'Active') {
           return redirect('billing');
        }

    }
}
return $next($request);

关于php - 用于订阅的 Laravel 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29998408/

相关文章:

php - 刚接触bind_param,这段代码有什么问题?

php - fsockopen() 返回空指针,$errstr 为空(无错误信息)

javascript - 每秒刷新一次php代码

php - MySQL - 存储货币符号的最佳方式?

laravel - 验证表单数组中的值

php - 在 null 上调用成员函数 setCookie() - Laravel 5.8

laravel - 为什么 Laravel 中间件会被执行多次?

php - jQuery 在 Laravel 5 中无法正常工作

php - unique_with laravel :It doesnt work if a particular row is ignored using id

php - Laravel 5.5 组中间件重定向你太多次