Laravel Backpack CRUDController return redirect() 或跳过中间件 运行 redirect()
Laravel Backpack CRUDController return redirect() or skip middleware run 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/.
我怎样才能以干净的方式做到这一点?
佩德罗·马丁斯@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);
}
如何从 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/.
我怎样才能以干净的方式做到这一点?
佩德罗·马丁斯@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);
}