对于SAVE和UPDATE使用相同的方法是不好的做法吗? [英] Is it bad practice to use the same method for SAVE and UPDATE?

查看:128
本文介绍了对于SAVE和UPDATE使用相同的方法是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用laravel,但并不重要,当您使用laravel命令行工具创建一个控制器时,它会将4个默认功能放在创建和更新中。



创建存储保存



编辑更新为<更新



这是店铺控制员的laravel建议。

  class ShopController extends Controller 
{

public function create()
{
//返回创建视图
}

public function store(request $ request)
{
// save a shop
}

public function edit($ id)
{
//找到一个商店,返回编辑视图
}

public function update(Request $ request,$ id)
{
//找到商店与ID,更新商店
}

}

但我喜欢使用相同的方法来显示视图和存储/更新我的行,并避免编写大量额外的代码。

  class ShopController extends Controller 
{

public function create($ id = 0)
{
return view('shop-create',['edit'=>店::发现($ ID)]);
}

public function store(Request $ request,$ id = 0)
{
$ whitelist = [
'title'=>'
'phone'=>'present | numeric',
'address'=>'present',
];
$ this-> validate($ request,$ whitelist);
$ shop = Shop :: findOrNew($ id);
//找到一个给定ID的商店或创建一个新的商店实体
foreach($ whitelist as $ k => $ v)
$ shop-> $ k = $ request [ $ K];

$ shop-> save();
}

}

自然,我跟我喜欢(第二选择),但是由于laravel建议第一种方式,出于好奇,有什么理由我不应该这样做吗?这是否被认为是不好的做法?

解决方案

没有错,但是你的代码将难以理解, p>

例如




  • 它被称为创建,但它也会编辑?

  • 视图称为 shop-create 但是它也会编辑?

  • 传递一个 0 参数作为ID的默认值,尝试 find 每次都不必要。



  public function create($ id = 0)
{
return view('shop-create',['edit'=> Shop :: find ID)]);
}

尽管您正在考虑简化代码,但您正在转动因为你经常从单一责任 nofollow> SOLID



如果您有类似Laravel的建议,最容易理解。



此外,您还可以保持任何Laravel开发人员都能理解的非常常见的模式,因此,您可以雇佣一个人来照顾您的代码,不必担心如果他明白。


I'm using laravel but it's not important, when you create a controller with laravel command line tool, it puts 4 default function in there for create and update.

create and store for save

edit and update for well update!

This is what laravel suggest for Shop controller.

class ShopController extends Controller
{

    public function create()
    {
       // return create view
    }

    public function store(Request $request)
    {
      // save a shop
    }

    public function edit($id)
    {
        // find a shop , return edit view
    }

    public function update(Request $request, $id)
    {
        // find the shop with id , update the shop
    }

}

But I like to use the same methods for showing view and store/update my row and avoid writing lots of extra code.

class ShopController extends Controller
{

    public function create($id  = 0)
    {
        return view('shop-create' , ['edit'=> Shop::find($id)]);
    }

    public function store(Request $request , $id = 0 )
    {
        $whitelist = [
            'title'=>'required',
            'phone'=>'present|numeric' ,
            'address'=>'present' ,
        ];
        $this->validate($request, $whitelist );
        $shop = Shop::findOrNew($id) ;
        // find a shop with given id or create a new shop instance
        foreach($whitelist as $k=>$v)
        $shop->$k = $request[$k];

        $shop->save();
     }

}

Naturally I go with what I like (second option), but since laravel suggest the first way, just out of curiosity is there any reason why I shouldn't do it like this? Is this considered bad practice in any way?

解决方案

Nothing wrong, but you code will be harder to understand, IMHO.

e.g.:

  • What do this method do? It's called create, but it also edit?
  • The view is called shop-create but it also edits?
  • Passing a 0 parameter as default for id and try to find it every time is unnecessary.

public function create($id  = 0)
{
    return view('shop-create' , ['edit'=> Shop::find($id)]);
}

Although you're thinking that you are simplifying your code, you are turning it more complicated since you are broking the Single Responsability principile from SOLID.

It's easiest to understand if you have something like the Laravel suggestion.

Also you keep a very common pattern that any Laravel developer will understand, so you can hire someone to take care of your code and do not worry if he will understand.

这篇关于对于SAVE和UPDATE使用相同的方法是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆