Laravel仅定义为仅限访客用户

时间:2017-10-09 10:16:25

标签: php laravel vue.js laravel-5.3 vue-component

我会为视频频道模型设置订阅系统。我使用Vuejs和laravel 5.3

在我的刀片模板中,我把这个

    <script>
   window.Laravel = <?php echo json_encode([
        'csrfToken' => csrf_token(),
    ]); ?>
    <script>

所有在Auth模式下运行正常(当用户登录时)但在访客模式下我的vuejs组件不起作用我在控制台中得到这个

ReferenceError: Laravel is not defined

截图 https://image.ibb.co/bPJCWG/laravel_Vuejs_Error_Guest.jpg 订阅者数量消失,无效工作

这是我的代码:

控制器:

public function show(Request $request, Channel $channel)
{
    $response = [
        'count' => $channel->subscriptionCount(),
        'user_subscribed' => false,
        'can_subscribe' => false,
    ];

    if($request->user())
    {
        $response = array_merge($response, [
            'user_subscribed' => $request->user()->isSubscribedTo($channel),
            'can_subscribe' => !$request->user()->ownsChannel($channel),
        ]);
    }

    return response()->json([
        'data' => $response
    ], 200);
}

SubscribeButton.vue组件:

<script>
export default {
    data () {
        return {
            //...
        }
    },
    props: {
        //...
    },
    methods: {
        getSubscriptionStatus () {
            this.$http.get('/subscription/' + this.channelSlug).then((response) => {
                this.subscribers = response.body.data.count;
                this.userSubscribed = response.body.data.user_subscribed;
                this.canSubscribe = response.body.data.can_subscribe;
            })
        }
    },
    mounted () {
        this.getSubscriptionStatus();
    }
}

请帮忙!

1 个答案:

答案 0 :(得分:1)

这是因为csrf仅适用于经过身份验证的用户。您需要更改您的javascript部分以考虑匿名用户,例如:

  

@if(Auth :: check())

     

//你的window.laravel js here

     

@endif