我正在寻找一种方法,仅向获得该视图链接的特定访问者显示特定视图。我如何制作一个中间件,以便仅在来自特定来源(如果它来自source.blade.php)时显示该视图
我不能将中间件用于来宾或身份验证,因为它会将该视图提供给所有身份验证,但我只想将该视图提供给已开始付款且已从特定URL重定向的身份验证。
如何设置中间件,只有在从另一个视图重定向auth时才显示视图,例如 - source.blade.php
目前,我已将此页面设置为此
public function __construct()
{
$this->middleware('auth:client');
}
这很有效,它只向从客户端身份验证防护登录的用户显示此页面,但问题是,任何客户端都可以访问此页面。
我正在寻找一种方法来制作它,以便只能由开始付款的客户查看,并由我的网站重新指导。也许像是
public function __construct()
{
if(redirect_source="source.blade.php") {$this->middleware('auth:client'); }
}
答案 0 :(得分:1)
我认为您需要的解决方案会根据您的用户类型限制权限。
如果您想让请求者进入特定网址/路由而不是控制您的视图内部,则使用中间件来调整某些参数。
因此,如果您想控制它,您可以使用此解决方案。
namespace App\Laravel\Middleware\Backoffice;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Auth, Session;
class ValidSuperUser {
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
* @return void
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($this->auth->user()->type != "super_user") {
Session::flash('notification-status','failed');
Session::flash('notification-title',"Access Denied");
Session::flash('notification-msg','You are not allowed to view the page you are tring to access.');
return redirect()->route('backoffice.dashboard');
}
return $next($request);
}
}
在Http文件夹下的Kernel.php中声明新的中间件以便使用。
**将其置于受保护的$ routeMiddleware = []
下然后将其用于需要帮助此类用户类型的路线。
$route->group(['middleware' => "aliasofyournewmiddle"],function(){
//some routes here
});
您的新中间件可以是请求时的任何条件,因此传递给该URL的任何输入和可用会话都可以在该中间件上使用,根据您希望的情况进行调整。
答案 1 :(得分:0)
您可以在将用户重定向到特定页面时传递令牌。然后使用中间件检查该令牌是否有效。
例如,某人在开始时付款,您在会话中存储该人的用户ID或任何唯一标识符的哈希值,然后使用相同的方式重定向用户您的网址中包含哈希值。如果会话中存储的值与url中提供的值相同,则中间件可以处理验证。