简体   繁体   中英

cakephp - how can I forward data in the controller to another controller?

I have a signup form posting to my customers_controller, but now they want the signup form to take payment information too. I have a payment_controller, After saving the new user I'd like to forward the payment post data to the action in the payment_controller.

How do I do that?

I would recommend that you put the actual logic for payment into a payment or invoices model. Doing that, you can either link the two models (allowing you to make a $this->Customer->Payment->process_charge(..) call), or you can define the $uses attribute in the Customers controller to allow calls to the Payment model.

I'm also a proponent of "fat models, skinny controllers" school of thought, in part due to this situation. I try to think of the controller as being in charge of the actual http request (access control, workflow properties, etc.) and letting the models do most of the heavy lifting.

I suggest that in the registration action in the users controller, when the user is registered (and logged in) you redirect to the payments controller where you use a normal payment model and view that self posts there. The way to communicate from the end of one action to another through a redirect would be to build the url. You do not need to pass along the user information however, since the Payments controller should be able to grab the user's id when needed, ie :

$this->data['Payment']['user_id'] = $this->Auth->user('id'); 
$this->Payment->create($this->data);
if ($this->Payment->save()) {
 // etc

Assuming Customers and Payments are related in some fashion, there's no reason you couldn't record payment information in your cusomters_controller.

$this->Customer->Payment->create()...

There's no rule that says you must use one controller for each model. In fact, that won't work in the vast majority of real-world applications like you're experiencing now.

I normally segregate my controllers by logical functionality groupings rather than trying to pair them up to models. In your case, I would build an accounts_controller (even if I have no accounts table) and put the login, registration, logout, profile editing, etc in that controller.

I find this sort of organization makes the application easier to maintain, and it makes for more logical paths for end users as well.

Ultimately I decided it was foolish to try and move data to another controller. I created a payment component which handles requests from any controller.

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