Laravel 4 Redirect :: back()不会转到上一页(刷新当前页面)

问题描述 投票:0回答:2

我有一个简单的Lavel 4项目,我正在尝试在提交信息后进行简单的重定向到上一页。但是,当插入Redirect::back()时,它只刷新当前页面。如何让它正确地重定向到它所在的上一页?

调节器

    public function saveCustomer($id) { 

    $zip_code = DB::table('zip_codes')
                    ->where('zip_code', Input::get('user_zip_code'))
                    ->first();  

    if (!empty(Input::get('user_id'))) {
        $user_id = Input::get('user_id');

        $user = User::find($user_id);

        $user->user_zip_code = $zip_code->zip_code;

        $user->office = $zip_code->office;

        $user->save();

    } else {
        $user_id = NULL;
    }       

    $userdata = [
                'user_id' => $user_id,
                'first_name' => Input::get('first_name'),
                'last_name' => Input::get('last_name'),
                'street_1' => Input::get('street_1'),  
                'street_2' => Input::get('street_2'),  
                'apartment' => Input::get('apartment'),
                'phone_1' => Input::get('phone_1'),  
                'phone_2' => Input::get('phone_2'),
                'state' => 'Alabama',
              'user_zip_code' => Input::get('user_zip_code'),
              'city' => $zip_code->city
            ];                
            $rules = array(
                'first_name' => 'required',
                'last_name' => 'required',
                'street_1' => 'required',
                'phone_1' => 'required'
            );

            $validation = Validator::make($userdata, $rules);

            if($validation->fails()){
                return Redirect::to("customer/edit/$id")->withErrors($validation)->withInput();
            } 

            UsersInformation::find($id)->update($userdata);

            return Redirect::back();

刀片模板

    {{ Form::open(array('name' => 'edit customer', 'action' => array('CustomerController@saveCustomer', $customer->id)))}}
       @foreach($errors->all() as $error)
                <p class="albox errorbox">{{$error}}</p>
            @endforeach
            <div class="four columns">      
        @if (isset($customer->first_name))
            {{ Form::hidden('user_id', '' . $customer->user_id . '')}}
        @endif                  
        <label class="form">First Name:</label> <span class="required"> (required)</span><br />
        @if (isset($customer->first_name))
            {{ Form::text('first_name', '' . $customer->first_name . '')}}<br />
        @else
            {{ Form::text('first_name')}}<br />
        @endif

        <label class="form">Last Name:</label> <span class="required"> (required)</span><br />
        @if (isset($customer->last_name))
            {{ Form::text('last_name', '' . $customer->last_name . '')}}<br />
        @else
            {{ Form::text('last_name')}}<br />
        @endif

        <label class="form">Primary Street:</label> <br />
        @if (isset($customer->street_1))
            {{ Form::text('street_1', '' . $customer->street_1 . '')}}<br />
        @else
            {{ Form::text('street_1')}}<br />
        @endif  

        <label class="form">Secondary Street:</label> <br />
        @if (isset($customer->street_2))
            {{ Form::text('street_2', '' . $customer->street_2 . '')}}<br />
        @else
            {{ Form::text('street_2')}}<br />
        @endif              

        <label class="form"> Primary Phone Number:</label> <br />
        @if (isset($customer->phone_1))
            {{ Form::text('phone_1', '' . $customer->phone_1 . '')}}<br />
        @else
            {{ Form::text('phone_1')}}<br />
        @endif

        <label class="form">Secondary Phone Number:</label> <br />
        @if (isset($customer->phone_2))
            {{ Form::text('phone_2', '' . $customer->phone_2 . '')}}<br />
        @else
            {{ Form::text('phone_2')}}<br />
        @endif          

        <label class="form">Apartment:</label><br />
        @if (isset($customer->apartment))
            {{ Form::text('apartment', '' . $customer->apartment . '')}}<br />
        @else
            {{ Form::text('apartment')}}<br />
        @endif
<label class="form">City/Zip Code:</label> <span class="required"> (required)</span><br />      
        <select name="user_zip_code"> 
            <option value='{{{ $customer->user_zip_code }}}'>{{{ $customer->city  }}} - {{{ $customer->user_zip_code }}}</option>
             @foreach ($zip_code as $zip_codes)
                <option value='{{ $zip_codes->zip_code  }}'>{{ $zip_codes->city  }} - {{ $zip_codes->zip_code  }}</option>
             @endforeach
        </select><br />         

<button type="submit" class="button is-success">Save</button><br /><br />
            </div>
{{ Form::close() }}

不确定是否需要,但这里也是我的route.php页面

Route::get('customer/edit/{id}', 'CustomerController@editCustomer');
php redirect laravel-4
2个回答
0
投票

按当前页面,是指表单所在的页面?如果是,那么它实际上是重定向 - 它转到控制器操作,处理saveCustomer()中的所有内容并重定向回到表单页面。

问题是......是否将用户保存在数据库中?

P.S你应该拥有模型中的所有数据库逻辑而不是控制器 - 使整个应用程序更模块化,更容易测试,控制器更薄。

编辑使用命名路由,并显式重定向到它。

routes.php文件

Route::get('/customer', array('as' => 'customer', 'uses' => 'MyController@showCustomer'));

Controller.php这样

function saveCustomer($id) {

    ...

    Redirect::route('customer');
}

0
投票

使用:

if(isset($_SERVER['HTTP_REFERER']))
    return Redirect::back();

return Redirect::to('/');
© www.soinside.com 2019 - 2024. All rights reserved.