【问题标题】:Laravel Backpack CRUDController return redirect() or skip middleware run redirect()Laravel Backpack CRUDController 返回 redirect() 或跳过中间件运行 redirect()
【发布时间】:2021-05-01 02:38:27
【问题描述】:

如何从 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/.

我怎样才能以干净的方式做到这一点?

【问题讨论】:

    标签: laravel laravel-backpack


    【解决方案1】:

    佩德罗·马丁斯 @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);
        }
    
    

    【讨论】:

      最近更新 更多