简体   繁体   English

了解Laravel和Cookie

[英]Making sense of Laravel and cookies

This is the first time I'm working with both cookies and Laravel. 这是我第一次同时使用cookie和Laravel。 I'm trying to store a simple value, sectionID , and retrieve it for the user so that they can continue working from their last visited section. 我正在尝试存储一个简单的值sectionID ,并为用户检索它,以便他们可以从上次访问的部分继续工作。 If there is no saved value, I want to return 0. 如果没有保存的值,我想返回0。

Server side routes: 服务器端路由:

Route::get('cookieget', function() {
    return Cookie::get('sectionID', 0);
});

Route::post('cookieset', function() {
     Cookie::make('sectionID', Input::get('sectionID'), 60*24);
});

Client side is quite complex but these are the relevant parts: 客户端非常复杂,但是这些是相关的部分:

UserController.js UserController.js

function UserController(userID) {
    var outer = this;

    ...

    this.setCookie = function(maxUserSectionID) {

        $.ajax({
            type: 'POST',
            url: "cookieset",
            data: {
                sectionID: maxUserSectionID
            },
            success: function(data) {
                console.log("Set max sectionID cookie to " + maxUserSectionID);
            }
        });
    }

    this.getSectionIDFromCookie = function() {
        $.ajax({
            type: 'GET',
            url: "cookieget",
            async: false,
            success: sectionController.handleNewSection
        });


    }
}

SectionController.js SectionController.js

function SectionController(editor, lessonNr)
{

    // setup initial data
    // the 0-based index of the current section
    this.currentSection = null;
    // the furthest the user has reached in this lesson
    this.maxSection = 0;
...

    // methods

    this.nextSection = function() {

        if (outer.currentSection > outer.maxSection) {
            outer.maxSection = outer.currentSection;
            userController.setCookie(outer.lessonJSON['sections'][outer.maxSection]['sectionID']);
        }

    ...
    };

    ...

    this.checkCookie = function() {
        userController.getSectionIDFromCookie();
    }

    this.handleNewSection = function(newSectionID) {
        alert(newSectionID);
        outer.currentSection = parseInt(newSectionID);
...
    }

    // called from outside as "constructor"
    this.setup = function() {
        ...
    outer.checkCookie();
    }

}

First of all, my main problem is that the cookie always returns 0, even though I'm sure that setCookie is called successfully. 首先,我的主要问题是,即使我确定setCookie调用成功,cookie始终返回0。

Second, I'm wondering if there's more elegant ways of doing it? 其次,我想知道是否还有更优雅的方法? I assume now it puts a lot of load on the server if isers are moving through sections too quickly. 我假设现在,如果isers太快地遍历各个部分,则会给服务器带来很多负担。

Must return a response with cookie to set your cookie properly: 必须返回带有cookie的响应才能正确设置cookie:

Route::post('cookieset', function() {
    $response = Response::make('Hello World');

    return $response->withCookie(Cookie::make('sectionID', Input::get('sectionID'), 60*24););
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM