Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

Laravel Backpack CRUDController return redirect() or skip middleware run redirect()

How can I run return redirect()->to('/') from the protected function setupListOperation() or any other method in CrudController.

As I check 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);
        });

the return $next($request); will not execute my return redirect();

now my current solution is die(redirect()->to('/'));, but the down site is the page will show the text

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/.

how can I do it in a clean way?

question from:https://stackoverflow.com/questions/65916137/laravel-backpack-crudcontroller-return-redirect-or-skip-middleware-run-redirec

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Pedro Martins @pxpm 21:24

@shiroamada if you want to redirect you have to overwrite the functions that should NOT be run by backpack, but should instead do your redirect. The ListOperation, is handled in index() method. So you need to overwrite the index() method of the ListOperation and do your redirects there. Here is how to overwrite the crud functions, change as needed: 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);
    }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...