WPBones icon indicating copy to clipboard operation
WPBones copied to clipboard

Add message to redirect

Open paradis-A opened this issue 6 years ago • 2 comments

How to add message to redirects

heres my sample code

    if($_POST!=null){
        $p = $_POST;
        $type  = new Roomtype();
        $type->name = $p['name'];
        $type->price = $p['price'];
        $type->maxpax = $p['maxpax'];
        $type->children = $p['children'];
        $type->adult = $p['adult'];
        $type->details = $p['details'];
        $type->save();
    }
    $this->redirect(get_admin_url().'admin.php?page=cdehotel_cde_roomtypes');
  }

is there a way to do it like ?

 return redirect('dashboard')->with('status', 'Profile updated!');

paradis-A avatar Sep 26 '19 07:09 paradis-A

@paradis-A Hi, not yet, but interesting

gfazioli avatar Jun 28 '22 06:06 gfazioli

@paradis-A Anyway, I think that you are already able to do the above flow easily.

You may check the demo branch of the WPKirk sample.

You will find an example of how to handle the POST in the View and Controller.

In short, in your case you could use the store method

<?php

namespace WPKirk\Http\Controllers\Dashboard;

use WPKirk\Http\Controllers\Controller;

class DashboardResourceController extends Controller
{
  public function load()
  {
    if( $this->request->get( '_redirect' ) ) {
      $this->redirect( $this->request->get( '_redirect' ) );
      exit;
    }
  }

  // GET
  public function index() {
    return WPKirk()->view( 'dashboard.optionsresview' )->with( 'method', 'GET' );
  }

  // POST
  public function store() {

    // process your own data and take nay action
    if($_POST!=null){
        $p = $_POST;
        $type  = new Roomtype();
        $type->name = $p['name'];
        $type->price = $p['price'];
        $type->maxpax = $p['maxpax'];
        $type->children = $p['children'];
        $type->adult = $p['adult'];
        $type->details = $p['details'];
        $type->save();
    }
    // check the results and errors

    // you may display the same view with new info
    return WPKirk()->view( 'dashboard.optionsresview' )->with( 'status', 'Profile updated!');

    // or you may display a completely different view or an error page
    return WPKirk()->view( 'dashboard.your_view' )->with( 'status', 'Profile updated!');

    // or you may display a completely different view or an error page - you may send any `$error` object/array with
   // all useful information
    return WPKirk()->view( 'dashboard.error' )->with( 'error', $error);

  }

  // PUT AND PATCH
  public function update() {
    return WPKirk()->view( 'dashboard.optionsresview' )->with( 'method', 'PUT AND PATCH' );
  }

  // DELETE
  public function destroy() {
    return WPKirk()->view( 'dashboard.optionsresview' )->with( 'method', 'DELETE' );
  }

}

gfazioli avatar Jun 28 '22 07:06 gfazioli

@paradis-A the above solution worked for you? have a look at https://wpbones.vercel.app/

gfazioli avatar Mar 25 '24 14:03 gfazioli