2

我在 Laravel 7 中工作,并且有一个中间件来检查用户是否有当前的用户协议,如果没有,它会重定向到提供当前协议的表单。当报价被接受时,我需要重定向回他们最初去的地方。我想我需要在会话中放一些东西,这样当我的控制器存储他们的接受时,它就可以重定向回原始路由。

class VerifyAgreement
{
    public function handle($request, Closure $next, $agreement)
    {
        if(UserAgreement::outOfDate($agreement)){
           return redirect()->route('agreement.offer', $agreement);
        }
        return $next($request);
    }
}

我想我需要获取当前请求并将其传递给重定向,以便用户协议控制器可以以某种方式捕获它,然后在存储协议后重定向......我不确定。

class AgreementController extends Controller
{
    public function offer(Agreement $agreement)
    {
        return view('agreement.offer',['agreement' => $agreement]);
    }

    public function agree(Request $request)
    {
        $agreement_uuid = App\Agreement::findOrFail($request->agreement)->uuid;
        UserAgreement::create(['user_uuid'=>auth()->user()->uuid, 'agreement_uuid'=>$agreement_uuid]);
        //redirect something something something
    }
}
4

1 回答 1

1

正如 @ 的评论中提到的Ruben DanielyanIlluminate\Routing\Redirector有一些你可能会觉得有用的方法

重定向器.php

/**
 * Create a new redirect response to the previously intended location.
 *
 * @param  string  $default
 * @param  int     $status
 * @param  array   $headers
 * @param  bool|null    $secure
 * @return \Illuminate\Http\RedirectResponse
 */
public function intended($default = '/', $status = 302, $headers = [], $secure = null)
{
    $path = $this->session->pull('url.intended', $default);

    return $this->to($path, $status, $headers, $secure);
}

/**
 * Set the intended url.
 *
 * @param  string  $url
 * @return void
 */
public function setIntendedUrl($url)
{
    $this->session->put('url.intended', $url);
}
于 2020-03-13T09:32:25.200 回答