如何在CRUD应用程序中成为更多Laravel? [英] How to be more Laravel in a CRUD app?

查看:74
本文介绍了如何在CRUD应用程序中成为更多Laravel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个关于如何使用出色的Laravel创建更好的应用程序的问题.我想创建具有用户权限的多种表单的CRUD应用程序.

This is a question about how to create a better application with awesome Laravel. I want to create a CRUD application with multiple forms with user's permissions.

详细信息:

  1. 查看.我有3种形式,例如让我们称之为catturtledog.最重要的是它们具有不同数量的字段. 这里是表格

  1. View. I have 3 forms, for example lets call it cat, turtle and dog. The most important is that they have different number of fields. Here are forms

控制器.用户填写表格并按下save按钮后,游戏中的控制器.我们所有人都知道,控制器越薄越好.但是我使用这样的构造:

Controller. After user filled in the form and pressed save button the controller in the game. All of us know that the thinner controller is the better it is. But I use construction like this:

switch($type)
{
    case '1':
        //form validation -> move to model
        //if all have passed that insert into database
        //Show user a message
        break;

    case '2':
        //form validation -> move to model
        //if all have passed that insert into database
        //Show user a message
        break;
//.....

    default:
        //show message
        break;

}

其中变量type等于用户填写的表单类型.但是,正如您现在所看到的,控制器很大,我不喜欢它.最好的方法是什么?

Where variable type equals form's type which has been filled in by a user. But as you can see now the controller is pretty big, and I don't like it. What the best way to do this?

模型. 我有4个表,分别是dogs_dataturtle_datacat_data,其中存储了表单中的所有数据,而pets_data中,存储了有关上述宠物的不同元数据.它是如何工作的?验证通过后,将数据插入这些表之一,并获取此插入的ID.然后在pets_data中创建记录,并将ID插入为pet_id,还插入table_type,这将是从数据库获取信息所必需的.我认为这是使用数据库的非常奇怪的方式. 此处是表格的可视化.

Model. I have 4 table, dogs_data, turtle_data, cat_data where I store all data from forms and pets_data where I store different metadata about pets above. How does it work? After validation passed insert data into one of these tables and get ID of this insertion. Then create the record in pets_data and insert the ID as pet_id and also insert table_type which will be required to get information from database. I think that it is very weird way to work with database. Here is table's visualisation.

另一个问题是-显示数据.例如,用户无权编辑数据或删除记录,但是管理员可以从数据库中删除,更新和查看大量数据.我是否应该创建另一个控制器(如AdminController)并在其中编写另一种仅显示管理信息的方法?

Another problem is - show data. For example user has no permissions to edit data or delete records, but admin can remove, updated and see a lot of data from database. Should I create another controller like AdminController and write there another method which display information only for admin?

我的应用程序现在:

  1. WebSiteController-显示网站
  2. AdminController-管理页面,有一种生成特殊页面以查看宠物的方法.
  3. UserController-用户控制面板,还有一种生成页面以查看宠物的方法,但对用户而言是特殊的.
  1. WebSiteController - display website
  2. AdminController - admin page, there is a method which generate special page to view pets.
  3. UserController - users control panel, there is also method which generate page to view pets, but special for user.

我认为要创建这样的东西

And I think to create something like this

  1. AuthController-专门用于登录\注册\激活\等
  2. PetsController-创建\删除\更新\等,并查看两个变体:useradmin.想法是创建一个方法getShowPets($pet_id)(重要的是它仅来自pets_data id ,而不是 pets_id ),然后获得用户的许可并生成页面.
  3. 以此类推,我想使用DRY规则重新编写我的应用程序

  1. AuthController - special for login\registration\activation\etc
  2. PetsController - create\delete\update\etc and also view in two variation: for user and admin. And idea is to create a method getShowPets($pet_id) (important it is only id from pets_data, not the pets_id) then get permission of the user and generate page.
  3. And so on, I want to re-write my application use DRY rule

因此,如果您能给我一些有关该项目的好提示,我将非常高兴.

So I will very happy if you can give me some good tips about this project.

推荐答案

您似乎正在建立多态关系,在这种情况下,我会将所有内容分开,这意味着您将拥有Cat,Dog,Turtle的模型和PetData.您将拥有CatController,DogController和TurtleController的控制器.而且您将拥有Cat表单,Dog表单和Turtle表单,每个表单还包含pet_info表所需数据的输入.

It looks like you are setting up a polymorphic relation, in which case, I would make everything separate meaning you'd have a model for Cat, Dog, Turtle, and PetData. You'd have controllers for CatController, DogController, and TurtleController. And you'd have a Cat form, a Dog form and a Turtle form, each that also contain inputs for the data you require for your pet_info table.

要创建多态关系,您的pet_data表将需要进行一些更改.只需将表列更改为pet_type. Laravel希望此列及其内容的特定名称是模型的名称,而不是表的名称.

To create the polymorphic relation, your pet_data table will require some changes. Just change the table column to pet_type. Laravel expects a certain name for this column and for its contents to be the name of a model rather than the name of your table.

为此设置模型非常容易,我将首先创建PetData和Dog.

Setting up the models for these is very easy, I'll do the PetData and the Dog one to get started.

class Dog extends Eloquent 
{
    protected $table = 'dogs_data';
    protected $timestamps = false;

    public function petData()
    {
        return $this->morphMany('PetData', 'pet');
    }
}

class PetData extends Eloquent
{
    protected $timestamps = false;

    public function pet()
    {
        return $this->morphTo();
    }
}

可以在此处阅读有关此内容的更多信息... http://laravel.com/docs/eloquent #polymorphic-relations

Can read more about this here... http://laravel.com/docs/eloquent#polymorphic-relations

为所有内容使用单独的模型/控制器的想法似乎需要很多工作,但是在尝试维护或向您的应用程序添加其他动物时,这将有很长的路要走,因为您几乎不必修改生产代码,从而省去了尝试在网站上添加增强功能时可能会引入更多错误的可能性.

The idea of having separate models/controllers for everything might seem like a lot of work, but it goes a long way when trying to maintain or add additional animals to your app because you should rarely have to modify production code, taking away the possibility of introducing more bugs when trying to add enhancements to your website.

现在,保存宠物和相关宠物数据变得非常容易,而不必担心pet_data表中的pet_id和pet_type,Laravel会为您处理.狗控制器上的功能可能看起来像这样...

Now it becomes very easy to save a pet and related pet data without having to worry about the pet_id and pet_type in the pet_data table, Laravel will take care of that for you. The function on your dog controller might look like this...

class DogController extends BaseController
{
    public function save()
    {
        $dog = new Dog;
        $dog->name = Input::get('name');
        $dog->age = Input::get('age');
        $dog->save();

        $pet_data = new PetData;
        $pet_data->color = Input::get('color');
        $dog->petData()->save($pet_data);
    }
}

至于为管理员创建另一个控制器,我会说,做到这一点.将您认为不同的网站部分保留在不同文件中,这无济于事.它不仅可以帮助组织,而且可以帮助您将关注点分离,这可能您应该了解更多.

As far as creating another controller for admins, I would say yes, do that. It never hurts to keep parts of your website that you consider different in different files. It not only helps with organization, but again, with separation of concerns, which you should probably read more about.

在管理角色方面,还有一些很棒的第三方选项.我以前使用过zizaco/entrust,发现它非常易于管理.尝试管理谁可以在您的Web应用程序上执行操作时,这可能会使您的生活变得容易得多.

There are also some fantastic 3rd party options for managing roles. I've used zizaco/entrust before and found it very easy to manage. It might make your life a lot easier when trying to manage who can do what on your web app.

这篇关于如何在CRUD应用程序中成为更多Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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