简体   繁体   中英

Laravel call function from another controller during validation for Paypal payment

I have a registration form (using Laravel 5 here), which upon submit calls BusinessController@postRegister

public function postRegister(Requests\RegisterBusinessRequest $request)
{
#1. get input, fill new class and save to database
#2. call PayPayController@postBusinessProvider
}

What I'm trying to do is call my PayPalController function to process payment, I've tried return Redirect::action('PayPalController@postBusinessProvider'); but that doesn't seem to work.

Do I have to simply have to create a route to call a function from another Controller or is there another way without creating a route? Or should I just put my PayPal code within the postRegister function, I figured cleaner to seperate?

In MVC model (and in Laravel) a controller will not call another controller in one request. To make your controller business simpler, you can use one of these two options:

Option 1

Separate a controller logic into small parts, each part is solve by a private or protected method of the same controller class. So that your controller will be something like this:

public function postRegister(Requests\RegisterBusinessRequest $request)
{
    $this->_validateInput();
    $this->_processInput();
    $this->_doWithPaypal();
}

Option 2

Create a repository class and to all complicated logic here. In your controller method, get the repository instance and pass your input data as the argument for this instance methods. You can see and example at zizaco/confide package. Your controller will something like this:

public function postRegister(Requests\RegisterBusinessRequest $request)
{
    $repo = App::make('PaypalRepositoryClass');
    // process your input
    // ...
    // ...
    $repo->paypalBusiness($data);
}

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