【问题标题】:Redirect doesn't work laravel 5重定向不起作用 laravel 5
【发布时间】:2017-03-08 13:10:05
【问题描述】:

路线:

Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
    Route::get('/',['uses'=>'Admin\IndexController@index','as'=>'adminIndex']);
    Route::resource('/cat-n-cat','Admin\CatalogsNCategoriesController');
});

控制器:

public function update($data)
    {
            $category = Category::find($data[0]['id']);
            $result = $this->category_rep->updateCategory($data,$category);    
            if (is_array($result) && !empty($result['error'])) {    
                return back()->with($result);
            }    
             redirect('admin')->with($result);    
    }

型号:

public function updateCategory($data,$category){
        $data=$data[0];
        if (empty($data)) {
            return array('error' => 'No data');
        }
        $result = $this->one($data['name']);            
        if (isset($result->id) && ($result->id != $category->id)) {
            return ['error' => 'Category with this name already exists'];
        }    
        $category->fill($data);    
        if($category->update()){    
            return ['status' => 'Category has been added'];
        }
    }

编辑类别重定向后不会触发,我停留在同一页面。如何修复它,它不起作用的原因是什么?

【问题讨论】:

  • 你试过这样返回redirect('admin')->with($result);
  • 还是不行:c
  • 你遇到了什么错误
  • 有什么错误吗?
  • @rahul_m 没有错误,只是仍然停留在同一页面,没有重定向

标签: php laravel redirect model controller


【解决方案1】:

返回重定向,

return redirect('/admin')->with(compact('result')); 

这里是link

它应该可以工作。

【讨论】:

  • 仍然不重定向我
【解决方案2】:

在顶部使用

use Illuminate\Support\Facades\Redirect;

public function update($data)
{
        $category = Category::find($data[0]['id']);
        $result = $this->category_rep->updateCategory($data,$category);    
        if (is_array($result) && !empty($result['error'])) {    
            return Redirect('<PreviousControllerName>')->with($result); //Change It
        }    
         return Redirect('/')->with($result);    //Change It
}

【讨论】:

  • 您是否在控制器中使用了“Admin”作为命名空间?
【解决方案3】:

你应该退货:

return redirect('admin')->with($result);

【讨论】:

  • 不,我没有收到错误,只是没有被重定向到 site.loc/admin
【解决方案4】:

能否请您检查以以下格式定义所有路线,然后尝试在适当的路线中重定向

Route::group(array('prefix' => 'admin','middleware'=>'auth'), function() {
    Route::post('student/cust_post', ['as' => 'admin.index', 'uses' => 'Admin\IndexController@index']
    );
});

【讨论】:

    【解决方案5】:

    with有两个参数

     return redirect('/admin/')->with('result',$result);
    

    【讨论】:

    • @Batmannn:你也尝试过adminIndex/admin/adminIndex 吗?
    • 都试过了,还是没有结果:c
    【解决方案6】:

    你试过了吗?

    return redirect()->route('adminIndex');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2016-05-15
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多