쿠키와 Laravel을 함께 사용하는 것은 이번이 처음입니다. 간단한 값인 sectionID
을 저장하고 사용자가 검색하여 마지막으로 방문한 섹션에서 작업을 계속할 수 있도록하려고합니다.Laravel 및 쿠키 이해하기
Route::get('cookieget', function() {
return Cookie::get('sectionID', 0);
});
Route::post('cookieset', function() {
Cookie::make('sectionID', Input::get('sectionID'), 60*24);
});
클라이언트 측이 꽤 복잡하지만, 이러한 관련 부분은 다음과 같습니다 : 더 저장 값이없는 경우에, 나는 0
서버 측 루트 반환 할
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
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();
}
}
우선, 내 주요 문제는 setCookie
이 성공적으로 호출되었다고하더라도 쿠키가 항상 0을 반환한다는 것입니다.
둘째, 더 우아한 방법이 있는지 궁금합니다. 지금 서버가 너무 빨리 섹션을 이동하는 경우 많은 부하를 서버에 가하는 것으로 가정합니다.
시도'위해서 var_dump (입력 : ('sectionID')를 얻을)은'cookieset' 노선에'? –