开发者

Proper place to put logic in MVC

开发者 https://www.devze.com 2023-02-27 09:50 出处:网络
I\'m writing an mvc web app with the yii framework.I have a piece of business logic and I\'m unsure where to place it.$usernameid=$model->random_id_gen(\'5\'); is the function I\'m talking about.

I'm writing an mvc web app with the yii framework. I have a piece of business logic and I'm unsure where to place it. $usernameid=$model->random_id_gen('5'); is the function I'm talking about.

SiteController:

<!-- snip -->开发者_C百科;
      public function actionIndex()
       {
         $model = new Users();

         if (isset($_POST['Users'])) {
            //call the active record table model
            $model = new Users();

            //massively assign attributes
            $model->attributes=$_POST['Users'];

            //generate random userid
            $usernameid=$model->random_id_gen('5');


<!-- snip -->

Users Active Record Class:

 <!-- snip -->
  public function random_id_gen($length)
    {
        $characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
        $max = strlen($characters) - 1;
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, $max)];
        }

        return $string;
    }

My question: Does this id generator function belong in the Active record model? Should it be in the controller? Should it be in a separate model since it is "business logic", but has little to do with databases?

I'm trying to get better at not "bloating" my MVC classes. Thanks ahead of time guys.

UPDATE I'm looking for a yii specific solution. It seems the question has developed into "where should one put libraries in yii" if there is such a convention.


MVC should be renamed to MVCL .. that is Model-View-Controller-Library. These kind of functions should go into a separate system of libraries.

Controller should be on the TOP. It has two major functions : accepting user input and coordinating other elemnts (controller / library/view) for output. traditionally, Controller asks data from the model. But data manipulation (most common) should be done by functions in the library.

There could be two levels of libraries, one could be complex (object based) and other could be simple (global functions based) for the most common operations. Something like stripping out tags, OR making the data XML ready, cleaning an URL etc.. simple functions libraries can be accessed by the whole system without any instantiation of the a particular library object.

Makes sense ?


I use to put libraries :

  • in an extension (/protected/extensions) for generic libraries
  • in protected/components if the library is generic to the project
  • in protected/modules/xxx/components for module specific libraries
  • in a vendors directory if the library is huge and I don't want to version it

Usually you import the protected/components (Yii::import('application.components.*') directory so these libraries would be available on demand.

This works fine for me.


I know that's quite seditionary. But since this specific functionality is not related to the database or operator logic (controller) and does neither interact with them, it simply does not belong in either object. Moreover it's a simplistic utility function.

So this is where it should go. (Unless you are intentionally doing CCP.)


As far as I know, and I have been using this myself, Yii Has a helper folder and thats the place I use to put helper functions like the one you asked about. I havent used it much since yii has sooo many functions covered, but I had to add a time function that would show the time until/ago for an event, that worked like a charm in the helper folder, and best part was Yii knew exactly where to look when I used Yii::import... hope that helps

0

精彩评论

暂无评论...
验证码 换一张
取 消