在客户端和服务器上集中发布数据

时间:2017-12-29 13:02:26

标签: laravel vue.js laravel-5.5

我正在建立我的第一个API,我有这个噩梦的场景,我看到自己在多个位置定义相同的请求数据。人们如何保持其关键/价值有效载荷?

这是我在客户端的VueJS组件:

<script>
    export default {
        data() {
            return {
                name: '', 
                description: '',
                selectedHeader: '',
            }
        },
        computed: {
            businessUrl() {
                return this.name.replace(/[^A-Z0-9]+/ig, '') + '.test.com';
            },
        },
        methods: {
            preview() {
                let data = {
                    'name': this.businessUrl,
                    'description' : this.description,
                    'selectedHeader': this.selectedHeader
                };

                axios.post('/builder/preview', data)
        ...
</script>

服务器端:

public function preview()
{
    $validatedData = request()->validate([
        'name' => 'required',
        'description' => 'string',
        'selectedHeader' => 'required',
    ]);

    $business = new Business; 
    $business->name = request('name');
    $business->description = request('description');
    $business->header = request('selectedHeader');

    return view('business', compact('business'));
}

如果我想更改名称&#39;在我的路线上发布的字段如果我在HTML中包含引用,我必须在最多5个地方执行此操作。人们为避免这种重复而开发的任何模式?

0 个答案:

没有答案
相关问题