Laravel 5.5使用锚点和数据重定向到url

时间:2018-01-13 12:03:44

标签: laravel laravel-5 laravel-5.5 laravel-form

在我的footer.blade.php中,我有以下有关订阅的代码

        <!--Subscription-->
        @if(Session::has('success'))
            <div class="form-group has-success">
                <input class="form-control form-control-success" type="text" id="input-success">
                <div class="form-control-feedback">{{ Session::get('success') }}</div>
            </div>
        @elseif(Session::has('failed'))
            <div class="form-group has-danger">
                <input class="form-control form-control-danger" type="text" id="input-danger">
                <div class="form-control-feedback">{{ Session::get('failed') }}</div>
            </div>
        @else
            {!! Form::open(['route'=>'subscribe.store', 'class' => 'subscribe-form', 'novalidate' => 'novalidate']) !!}
            <div class="clearfix">
                <div class="input-group input-light">
                    {!! Form::email('email', old('email'), ['class'=>'form-control ', 'placeholder'=>'Your e-mail']) !!}
                    <span class="input-group-addon"><i class="icon-mail"></i></span>
                </div>
                <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
                <div style="position: absolute; left: -5000px;" aria-hidden="true">
                    <input type="text" name="b_c7103e2c981361a6639545bd5_1194bb7544" tabindex="-1">
                </div>
                <button class="btn btn-primary" type="submit">
                    <i class="icon-check"></i>
                </button>
            </div>
            {!! Form::close() !!}
            <span class="form-text text-sm text-white opacity-50">Subscribe to our Newsletter to receive early discount offers, latest news, sales and promo information.</span>
        @endif

在我的控制器中,我有以下功能接收电子邮件并正确订阅

public function registerNewsletter( Request $request )

{

    $email = $request->input( 'email' );
    $isSub = Newsletter::hasMember($email);
    if($isSub) {
        return redirect()->back()->with('failed', 'You have been already registered in our Newsletter system.');
    } else {
        Newsletter::subscribe($email);
        return redirect()->back()->with('success', 'You have been registered in our Newsletter system.');
    }

}

虽然一切正常,但我想翻页向下滚动到页脚,因为现在它重新加载并保持在最顶层。

我找到了return redirect()->to(app('url')->previous(). '#hashid');

但是无法在页脚的id之间传递数据。

我也试过

return redirect()->back()->with('failed', 'You have been already registered in our Newsletter system.'). '#sitefooter'; 它没有向下滚动。

任何想法或解决方法?

1 个答案:

答案 0 :(得分:1)

我刚刚测试了这段代码。它将重定向您,向下滚动到锚点并传递数据:

session()->flash('someVariable', $someData);
return redirect(url()->previous() . '#hashid');

获取存储的数据:

$someData = session('someVariable');

或者在视图中:

{{ session('someVariable') }}