douba3943 2013-11-26 07:27
浏览 14
已采纳

我应该如何在Codeigniter中布置我的代码?

Currently I'm doing something like this:

Controllers: Immediately call models and then views. No real other logic than deciding which model and view to call

Models: Contain all functions per table in my db. I have one model class with its methods per table in my db.

View: Only contain layout, almost zero logic.

Libraries: All my classes that aren't specific to a database table

Third Party: Downloaded plugins

My question is, is this correct? For instance, should I always put my non-table-specific classes in libraries? What about in cases where I have a class or function that is a combination of, say, "product" and "customer" tables?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • doudeng5764 2013-11-26 21:57
    关注

    Structure your applications based on Roles (sometimes called Actors). in other words you don't structure a controller based on "editing" - you structure it based on an "Editor" and what they need to do. This might sound like word games but its actually very profound because the "tool" should serve the people using them.

    For example if you have an Editor - you then create "use cases" for the editor - what the actor needs to do and what assets they are acting upon. Your methods then flow from these use cases. This also greatly simplifies security. You are not creating a controller that serves 5 different users with 5 different security configs. You just have an Editor with their unique security - once you have established they are credentialed (logged in) - you don't have to check for it over and over again.

    Naming is critical. It is the key difference between an easy to understand application and a confusing one. Allow yourself to change and refine your controller, model, method names so that they become more clear. You should be able to look in your models folder, look at the names of the models - and know exactly what the application does.

    A method should do one thing and do it well. If your method does 3 different things, split it up into 3 methods.

    Split up your models as they get bigger and then arrange with folders. Which is going to be easier - finding a method in a 1000 lines of code or in 300? splitting up your models makes it much faster because then you can also be more specific in the naming of the model. its perfectly fine to have a model that does just one thing if that one thing is complicated.

    personally if you are using CI - i would suggest concentrating on building out models versus libraries. another suggestion is to build in the controller - then push the code to models as you finalize. so like maybe a contact form - you build the validation in the controller to have all the code together and easy to get to. you finish it, then push the validation code to its own model.

    (you mentioned products / customers - codeigniter is awesome for e-commerce)

    =====

    edit

    Naming - (shakes head) yeah i'm still learning. One thing i will say - I do not think its a good practice to put the word "model" in your model names like

     $this->user_model->getUser($id) ; 
    

    for a while i thought this pattern was really kewl

     $this->user_m->getUser($id) ; 
    

    and i wrote a post about how cool it was. then i realized it was not cool at all and deleted the post!! think about it - you don't call other controllers from your controller. you only call models. so why repeat the word 'model' everywhere?

     $this->user->getUser($id) ; 
    

    so thats how i would probably start. but now we want to try and make it readable so how about

    $this->user->getBy($id) ; 
    

    "this user get by ID" -- its pretty clear what is going on. you can read it out loud and it makes sense. that is the key test. that also means that a new person can look at your code and understand it much faster.

    martin makes a big deal out of reading code out loud - at first it might sound silly, but it has huge implications for how maintainable your code is long term.

    one thing i go back and forth about - should it be "get" or "return" - i usually use get because its short and direct.

    i would use Find if you are not sure if there is going to be a user with that id. if you are getting a group of things - like searching for a product - and there might not be any results, then maybe searchFor($item) or similar.

    again i would watch every robert martin video you can. and keep trying out different naming conventions. say them out loud. the other angle on this is you will have to write far less comments.

    ok here is another guideline - write out your variable names so they make sense. do not abbreviate them.

     $first // NO why? first what?? 
    
     $firstname or $first_name or $firstName // yes first name 
    

    here are some robert c martin rules on naming

    Methods in small scopes should have long and precise names. Methods in long scopes should have general names.

    You should not have to read the body of a method to know what it does. It's name should tell you.

    The more complex the behavior of a method, the more generic its name, and the more sub methods should be extracted from it.

    note that - in his lectures etc i have never heard robert martin say the letters php. he probably does not consider it a real language. but what is awesome is that his examples are primarily in java and the syntax is very similar to writing methods in codeigniter. (so don't worry that you are not a java programmer)

    here are some interesting responses to the same question

    https://softwareengineering.stackexchange.com/questions/119345/meaningful-concise-method-naming-guidelines

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备