phpmodel-view-controllercakephpcontroller

Controller logic in Element View in CakePHP


I'm working on a really big project. The aspect I'm currently working on requires that email templates are sent to a user when they're added to a learning course by another user.

The controller that deals with the request, does a bunch of str_replace tasks to find variables in the text (which the user can edit before adding another user to the learning course) and then replaces it with some values in the DB.

I took over this project and I'm not happy with the way half the things are done but time costs dictate I rather just go along with it.

The email is sent using Cake's native email function. It uses a template to capture data and send to the user.

Here's the question:

  1. Should I keep the logic in the controller or do you think it's safe to move it to the element view's .ctp file?

My first instinct is to leave it in the controller as per the usual MVC separation ideals.

Cheers


Solution

  • This is a very important question - what are you using exactly for the email? The old email component or the new CakeEmail class? Which CakePHP core version are you using?

    Here are some plausible aproaches here. You can:

    1. Set all those variables, pass them to the view and do all the "replacing" there.
    2. Encapsulate this logic in a component, attach it to your controller(s) and use it.
    3. Just leave it in a private function within the controller and call that function whenever needed. (not really MVC)