Hi,
I’m building a like button on my project pages.
Inspired by the code of the likes plugin, I created a custom route to catch any “project/like” url and increment the likes variable of the project
[
'pattern' => '(:any)/like',
'action' => function ($slug) {
if ($page = page($slug)) {
$counter = $page->increment('likes',1)->content()->get('likes');
$page->update([ 'likes' => $counter ]);
return strval( $counter );
}
}
]
In my main.js, a press on the like button fires an ajax load as such :
var mySlug = $btnLike.attr('data-slug');
$btnLike.children('.number_like').children('p').load("/"+mySlug + "/like", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
$('.is-selected').attr('data-likes',responseTxt);
console.log("External content loaded successfully!");
$btnLike.children('.number_like').children('p').html(likesFormatter(Number(responseTxt)));
if(statusTxt == "error")
console.log("Error: " + xhr.status + ": " + xhr.statusText);
});
The issue : It only works when I am logged in to the panel. Otherwise (when mysiteUrl/panel opens the login form) I get a 500 Internal server error as a response when I press the like button.
Any idea on how to fix this ?
Thanks
Julien