dsa88885555 2013-08-12 18:27
浏览 23
已采纳

处理具有类似操作的路线的最佳做法是什么? [关闭]

I'm working on a picture gallery web application. It has many access routes, they all should render a gallery the same way, the only difference is the order of the pictures.

so for example:

mysite.com/favorites -> shows all the pics ordered by number of favorites
mysite.com/views -> shows all the pics ordered by number of views
mysite.com/date -> shows all the pics ordered by date
mysite.com/votes -> shows all the pics ordered by votes

Being all these actions so similar between them, Is it a good practice to define one action per route on the controller and pass the order as parameter? Or this logic should be handled in the model?

  • 写回答

1条回答 默认 最新

  • duankui6150 2013-08-12 20:16
    关注

    If actions are so similar that differ from a parameter, it could make senss to have one action that accept that parameter. That last is not a rule, just a preference.

    In your specific case you can handle it as follows by using one route with parameter:

    Route::get('{orderBy}', 'GalleryController@showPics')
    

    Then in your controller you will have something as follows:

    class GalleryController extends BaseController
    {
        public function showPics($orderBy)
        {
            $pics = $this->service->getPics($orderBy);
            return View::make('gallery')->with('pics', $pics);
        }
    }
    

    Though it will work fine, there is some points to consider:

    1. It is more semantic to have controller with specialized action i.e.: showFavorites, showMostViewed, showRecents, showMostVoted.
    2. Think about the view. If you plan to deliver different view, different nested child with different data set, it is better to have an action for each case.
    3. Code readability. Imagine the controller with several if statements, several return statements, all wrapped in a big try-catch block that have multiple catch clauses... If you can avoid a brain lapse to the next programmer with a spaghetti code, consider it.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题