【问题标题】:Laravel 4 Redirect::back() not going to previous page (refreshing current page)Laravel 4 Redirect::back() 不会转到上一页(刷新当前页面)
【发布时间】:2013-12-27 09:25:42
【问题描述】:

我有一个简单的 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


    【解决方案1】:

    用途:

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

    【讨论】:

      【解决方案2】:

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

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

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

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

      Routes.php

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

      控制器.php

      function saveCustomer($id) {
      
          ...
      
          Redirect::route('customer');
      }
      

      【讨论】:

      • 问题不是为什么它会重定向到同一个页面,而不是用户之前所在的页面。
      • 如果我理解得很好:从A页到B页(表单页),提交表单并期望返回到A页?如果是这种情况,则您缺少页面 C(控制器页面),其背面是页面 B,而不是页面 A。为此使用预期的或特定的命名路由。
      • 它确实有控制器。 Route::get('customer/edit/{id}', 'CustomerController@editCustomer'); ..so 基本上当我在根目录(/)并转到(客户)并保存表单时。它将通过该路由editCustomer的控制器运行,然后返回给(客户)我需要它去(/)
      • 应该是这样的。控制器方法 saveCustomer() 是一个单独的页面,与“/”或“/customers”完全一样。尝试一下——在 saveCustomer() 的开头设置一个 return 并自己看看。
      • 将它设置在控制器的开头会做同样的事情。它转到后备页面
      猜你喜欢
      • 1970-01-01
      • 2022-01-14
      • 2020-04-03
      • 2018-09-15
      • 2011-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      相关资源
      最近更新 更多