奇怪的令牌与Laravel和Vue不匹配

时间:2017-05-10 14:54:35

标签: php laravel vue.js vue-router

我设置的两条路线是:

POST     | forums/thread/create                 | game.forums.create      | Biolumites\Http\Controllers\Game\Forums\CreateController@postCreate          | web
POST     | register                             | game.auth.register      | Biolumites\Http\Controllers\Game\Auth\RegisterController@postRegister        | web

两个控制器都存在。

注册路由完全符合要求。

我正在使用vue-router显示当前视图:

<template>
    <div class="content__inner">
        <div class="forums" v-if="categories">

            <!-- Heading -->
            <div class="forums__heading">
                <div class="lg-12 md-12 sm-12 column column__first">
                    <h2 class="forums__heading__title">{{ translations.title }}</h2>
                </div>
                <div class="clearfix"></div>
            </div>

            <!-- Content -->
            <div class="forums__content">
                <div class="forums__content__item">
                    <form method="POST">
                        <!-- Title -->
                        <label class="form__label" for="title">{{ translations.forms.title.title }}</label>
                        <input class="form__input" name="title" v-model="form.title">
                        <!-- Board -->
                        <label class="form__label" for="board_id">{{ translations.forms.board.title }}</label>
                        <select class="form__input" name="board_id" v-model="form.board">
                            <optgroup v-for="category in categories" :label="category.title">
                                <option v-for="board in category.boards" :value="board.id">{{ $parent.layout.boards[board.slug] }}</option>
                            </optgroup>
                        </select>
                        <!-- Content -->
                        <label class="form__label" for="content">{{ translations.forms.content.title }}</label>
                        <tinymce id="content" name="content" v-model="form.description" :options='options'></tinymce>
                        <br>
                        <!-- Submit -->
                        <input class="button background-transparent-green" type="submit" :value="translations.forms.submit" @click.prevent="submit('/forums/thread/create', '/forums/board/' + board_slug)">
                    </form>
                </div>
            </div>

        </div>
    </div>
</template>

<script>

    import form from '../../mixins/form.js';
    import tinymce from '../../mixins/tinymce.js';
    import translations from '../../mixins/translations.js';

    export default {

        mixins: [form, tinymce, translations],

        data() {
            return {
                form: {
                    title: null,
                    board: parseInt(this.$route.query.board),
                    description: null,
                },
                board_slug: null,
                categories: null,
            }
        },

        created() {
            this.fetch_categories();
        },

        methods: {

            /**
             * Fetch all the categories.
             */
            fetch_categories()
            {
                axios.get('/api/forums/categories').then((response) => {
                    this.categories = response.data;
                });
            },

            /**
             * Fetch the slug of the currently selected board.
             */
            fetch_board_slug()
            {
                for(var category_index = 0, len = this.categories.length; category_index < len; category_index++)
                {
                    for(var board_index = 0, len = this.categories[category_index].boards.length; board_index < len; board_index++)
                    {
                        if(this.form.board == this.categories[category_index].boards[board_index].id)
                        {
                            this.board_slug = this.categories[category_index].boards[board_index].slug;
                        }
                    }
                }
            },

        },

        watch: {

            /**
             * Watch the categories.
             */
            'categories':
            {
                handler: function()
                {
                    this.fetch_board_slug();
                }
            },

            'form.board':
            {
                handler: function()
                {
                    this.fetch_board_slug();
                }
            },

        },

    }

</script>

当我使用/register路线提交表单时,我按预期点击了路线(当我登录时未经授权的响应)。但是,当我提交完全相同的表格时,我会以同样的方式获得500错误和使用/forums/thread/create路线的令牌不匹配。

查看回复Cookie,XSRF-TOKEN路由正在传递/register,而/forums/thread/create路径则没有。

为什么会发生这种情况,如何解决这个问题呢?

0 个答案:

没有答案