Controllers+JS: Change the blog search to a POST request
This commit is contained in:
@@ -9,29 +9,23 @@ class BlogController extends PageController {
|
|||||||
public function indexAction(): void
|
public function indexAction(): void
|
||||||
{
|
{
|
||||||
$archived = $this->router->request()->param('archived', 0);
|
$archived = $this->router->request()->param('archived', 0);
|
||||||
$query = $this->router->request()->param('search', '');
|
$search = $this->router->request()->param('search', '');
|
||||||
$posts = $this->search($query, $archived);
|
$posts = $this->search($search, $archived);
|
||||||
|
|
||||||
$this->defineHelpers();
|
$this->defineHelpers();
|
||||||
|
|
||||||
$this->router->service()->search = $query;
|
// AJAX search request will only render the posts partial
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||||
|
$this->router->service()->partial($this->views . '/partials/blog-posts.php', ['posts' => $posts]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->router->service()->posts = $posts;
|
$this->router->service()->posts = $posts;
|
||||||
|
$this->router->service()->search = $search;
|
||||||
$this->router->service()->injectView = $this->views . '/partials/blog-posts.php';
|
$this->router->service()->injectView = $this->views . '/partials/blog-posts.php';
|
||||||
parent::view('blog', 'Hello');
|
parent::view('blog', 'Hello');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function searchAction(): void
|
|
||||||
{
|
|
||||||
$archived = $this->router->request()->param('archived', 0);
|
|
||||||
$query = $this->router->request()->param('query', '');
|
|
||||||
$posts = $this->search($query, $archived);
|
|
||||||
|
|
||||||
$this->defineHelpers();
|
|
||||||
|
|
||||||
$this->router->service()->posts = $posts;
|
|
||||||
$this->router->service()->partial($this->views . '/partials/blog-posts.php', $posts);
|
|
||||||
}
|
|
||||||
|
|
||||||
//-------------------------------------//
|
//-------------------------------------//
|
||||||
|
|
||||||
private function search(string $query = '', int $archived = 0): ?array
|
private function search(string $query = '', int $archived = 0): ?array
|
||||||
|
|||||||
+7
-1
@@ -37,7 +37,13 @@ $(document).ready(function() {
|
|||||||
var url = $(this).data("url");
|
var url = $(this).data("url");
|
||||||
var search = $(this).val();
|
var search = $(this).val();
|
||||||
if (search.length == 0 || search.length >= 3) {
|
if (search.length == 0 || search.length >= 3) {
|
||||||
fetch(url + '/search?query=' + encodeURIComponent(search))
|
fetch(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: 'search=' + encodeURIComponent(search),
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
}
|
||||||
|
})
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
$("#blog-posts").empty().append(data);
|
$("#blog-posts").empty().append(data);
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ return [
|
|||||||
['/robots.txt', 'IndexController', 'robots'],
|
['/robots.txt', 'IndexController', 'robots'],
|
||||||
['/sitemap.xml', 'IndexController', 'sitemap'],
|
['/sitemap.xml', 'IndexController', 'sitemap'],
|
||||||
['/blog', 'BlogController'],
|
['/blog', 'BlogController'],
|
||||||
['/blog/search', 'BlogController', 'search'],
|
|
||||||
['/login', 'LoginController', 'login', ['', 'Sign in', '']],
|
['/login', 'LoginController', 'login', ['', 'Sign in', '']],
|
||||||
['/reset-password', 'LoginController', 'reset', ['', 'Reset password', '']],
|
['/reset-password', 'LoginController', 'reset', ['', 'Reset password', '']],
|
||||||
['/logout', 'LoginController', 'logout'],
|
['/logout', 'LoginController', 'logout'],
|
||||||
|
|||||||
Reference in New Issue
Block a user