Should controllers avoid private functions at CakePHP?

I was wondering if controllers at CakePHP should not contain any private function which is not accessible by URL.

Sometimes some functions such as add or delete can be so large that I prefer to divide them. Should I put that functions inside the model instead of making then private on the Controller?

Thanks.

Answers


Yeah it's probably for the best if you keep your methods in the Model. Like you yourself have mentioned in the comment, "keep models fat and controllers thin".. The controller is just an medium which is supposed to interact between model and view.

The problem comes when you have to deal with change in datasources, tables. If your controllers are fat, you would have used the fields everywhere, now you will be left with cleaning the whole setup, in a place where it wasn't supposed to be.

The added benefit of methods in models is that you can call it from other models and REUSE the code. for example

class User extends AppModel {

     public function getAllActiveUsers() {
         // return active users
     }

} 

The above method can be accessed by every other methods in both model and controller which is related to the User.

If you need such functions elsewhere and if you don't have them defined in the User model, you will either end up redirecting it to the controller or write the whole logic all over again.

The redirecting thing isn't that bad but consider if u have rewritten the logic elsewhere and if your implementation of ActiveUsers changes, u will end up correcting everywhere.

However, there are few things that has to be done in controller. For example, if i have to calculate the distance between the user's geolocation and all the matching restaurants for a nearby distance I should do this in the controller. But it is in the best of interest the controller are left thin, for this purpose there exists components. You can create your custom components for complex and lengthy logic.


I put lower-level functions in the models. Especially if the function can be used by multiple controllers. By "low-level" I mean as close to the model data as possible. If you're going to be doing view-specific modifications to the data or combining it with data from other models (related models are an exception), the function does not belong in the model.

Also, you can just prefix any controller function with an underscore and it won't be available via the url.


The logic, that deals with adding and removing entries from database are part of domain business logic. Such methods should be part of the model layer.

Since CakePHP's implementation is extremely limited (it pretends that model layer is collection of active record instance), you might move those methods to some sort of helpers, or use separate "models", which do not inherit from AppModel. Instead such structures would be able to act like services, which isolate controller (and presentation layer as whole) from the domain business logic.


No. CakePHP is a framework, feel free to create private functions for reusable bits of logic. It's probably encouraged.


Need Your Help

Executing a Command from Java and Waiting for the Command to Finish

java batch-file command exec execute

In my Java program, I create a process that executes a command to run a batch file like this:

NSLocale identification

objective-c nslocale

Can anyone please tell me why the following code gives the opposite result in a machine with en_US locale identifier ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.