Laravel Backpack CRUDController返回redirect()或跳过中间件运行redirect(



如何从protected function setupListOperation()或CrudController中的任何其他方法运行return redirect()->to('/')

当我检查CrudController

$this->middleware(function ($request, $next) {
$this->crud = app()->make('crud');
$this->crud->setRequest($request);
$this->setupDefaults();
$this->setup();
$this->setupConfigurationForCurrentOperation();
//COMMENT: as I return from the setupListOperation, then it will run the below return $next.
return $next($request);
});

return $next($request);不会执行我的return redirect();

现在我当前的解决方案是die(redirect()->to('/'));,但下面的网站是页面将显示文本

HTTP/1.0 302 Found Cache-Control: no-cache, private Date: Wed, 27 Jan 2021 09:16:52 GMT Location: http://backpack.test/admin/ Redirecting to http://backpack.test/admin/.

我怎样才能用干净的方式来做呢?

Pedro Martins @pxpm 21:24

@shiroamada如果你想重定向,你必须覆盖不应该由背包运行的函数,而应该做你的重定向。ListOperation在index()方法中处理。因此,您需要覆盖ListOperation的index()方法并在那里进行重定向。下面是如何覆盖crud函数,根据需要进行更改:https://backpackforlaravel.com/docs/4.1/crud-operation-update#callbacks

public function index()
{
//custom code for redirect
if(!$this->crud->getCurrentEntryId())
{
$previous_url = parse_url(url()->previous());
$member_id = ltrim($previous_url['query'], 'member_id=');
return redirect('/admin/member/'.$member_id.'/show');
}
//.custom code for redirect

//original index code
$this->crud->hasAccessOrFail('list');
$this->data['crud'] = $this->crud;
$this->data['title'] = $this->crud->getTitle() ?? mb_ucfirst($this->crud->entity_name_plural);
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view($this->crud->getListView(), $this->data);
}

最新更新