简体   繁体   中英

Laravel call a controller within a controller

I have 2 controllers, one is responsible for submitting a form to the DB, the other is for PayPal integration such as this one:-

http://laravelcode.com/post/how-to-integrate-paypal-payment-gateway-in-laravel-54

I want it so that when the user presses the submit button, it does its usual DB transactions but then calls the PayPal controller to process the payment.

Is it better merge the 2 controllers into one or to call the PayPal controller as part of the store method??

You can call another controller using the following method.

$controller = app()->make('App\Http\Controllers\PaypalController');
app()->call([$controller, 'process'], [$request]);

Where your controller function is defined as:

public function process(Request $request) {}

While not the greatest practice, I have used this for calling a function referenced in a console command and in a URL.

In the function which do the DB transactions, try redirecting to your paypal function:

public function myDBFunc() {
    /* do transactions */

    return redirect()->route('paypalRoute');
    // or return redirect()->action('PaypalController@paypalFunc');
}

Do not forget to pass your variables to your route/action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM