Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Simple Search plugin - getting error 404 of misconfigured URL's

Solved by pamtbaau View solution

Started by 01K 5 years ago · 15 replies · 1321 views
5 years ago

I'm building a blog and I've added a simple search plugin.
As for a example I've taken a skeleton with search and blog modules.

Let's assume, that my blog has this URL - /blog

blog.html.twig uses standard URL expressions based on other examples:

TWIG
{% embed 'partials/base.html.twig' %}

    {% set collection = page.collection() %}    
    {% set base_url = page.url %}    

    {% if base_url == '/' %}
        {% set base_url = '' %}     
    {% endif %}

    {% block content %}

    {{ page.content }}

        <section class="container-fluid">
            <div class="row">
                <div class="col-md-8">  
                    {% for child in collection %}
                        {% include 'partials/blog_item.html.twig' with {'page':child, 'truncate':true} %}               
                    {% endfor %}
                </div>
                {% include 'partials/blog_sidebar_footer.html.twig' %}
            </div>
        </section>
        {% if config.plugins.pagination.enabled and collection.params.pagination %}
            {#% include 'pagination.html.twig' with {'pagination':collection.params.pagination} %#}
            {% include 'pagination.html.twig' with {'base_url':page.url, 'pagination':collection.params.pagination} %}
        {% endif %}

    {% endblock %}

{% endembed %}

blog_sidebar_footer.html.twig is a simple wrapper:

TWIG
<div class="col-md-4" id="side-bar">
    {% include 'partials/sidebar.html.twig' %}
</div>

And sidebar.html.twig:

TWIG
{% for module in page.collection({'items':{'@taxonomy.category': 'sidebar'},'order': {'by': 'default', 'dir': 'asc'}}) %}
    {% if module.template %}
        {% if module.header.surround %}
            {% include 'sidebar/' ~ module.template ~ '.html.twig' %}
        {% endif %}
    {% endif %}
{% endfor %}

<div class="sidebar section" id="sidebar">
{% for module in page.collection({'items':{'@taxonomy.category': 'sidebar'},'order': {'by': 'default', 'dir': 'asc'}}) %}
    {% if module.template %}
        {% if not module.header.surround %}
            {% include 'sidebar/' ~ module.template ~ '.html.twig' %}
        {% endif %}
    {% endif %}
{% endfor %}

{% if config.plugins.simplesearch.enabled %}
<div class="widget HTML">
  <h2 class="title">Search</h2>
  <div class="widget-content simplesearch">
    {% include 'partials/simplesearch_searchbox.html.twig' %}
  </div>
</div>
{% endif %}
</div>

So, once I enter some data in search box I get Error 404
The full URL looks like: https://localhost/grav-blog/blog/search/query:test

Exploring examples I assume, that URL should be without /blog/ slug -> https://localhost/grav-blog/search/query:test

Once I enter URL manually https://localhost/grav-blog/search/ I see Search Results page, but if I add a query -> array_replace_recursive(): Expected parameter 2 to be an array, string given

What I'm missing? I already tried overwriting all templates with the ones from demo skeletons - but I had no luck...

5 years ago

@01K, What is the value of route in config file /user/config/plugins/simplesearch.yaml?

Is it route: /search or route: /blog/search ?

5 years ago

The route is route: /search
If I make route: /blog/search the search result’s URL will look like:
https://localhost/grav-blog/blog/blog/search/query:test

I've downloaded a Pin Press Blog skeleton as for example and copied the template overrides with twig part.
These variables from blog.html.twig outputs different results:

TWIG
{% set base_url = page.url %} 

Once I run example on localhost and output

TWIG
{{ base_url }}
{{ page.url }}

I get /grav_blog and /grav_blog paths.

Once I run it from mine project I get:
/grav-blog/blog and /grav-blog/blog

The main difference is that on mine project I've been using Gantry5 framework for template part, if it make any difference. Don't know why the {{ base_url }} and {{ page.url }} are changed... I've already tried to fully overwrite mine templates with templates from example, as for a try, but it acts the same...
System settings (system/config/system/yaml) are untouched:

YAML
absolute_urls: false   
custom_base_url: ''
last edited 01/17/21 by 01K
5 years ago

update
I don't know why, but changing the {% set base_url = page.url %} in
blog.html.twig to {% set base_url = page.parent.url %} resolves the issue on a blog items list page. I don't know - is it correct...
The problem still persist on item.html.twig page where base_url settings are the same:

TWIG
{% set base_url = page.parent.url %}
{% if base_url == '/' %}
{% set base_url = '' %}
{% endif %}

If I will find a sulution, I will update the post

5 years ago

I've made a step forward resolving this issue.
I had to change this line in system.yaml:
home:
alias: /home into-> /blog

In such way the slug is not added in search URL, BUT, I'm loosing my previous home page!
Is it a bug in a search plugin?

5 years ago

@01K, I tried to reproduce your issue, but can't....

I did the following:

  • Fresh install of Grav 1.7.0-rc.20
  • Using theme Quark
  • Installed simplesearch: $ bin/gpm install simplesearch
  • Created a blog folder:
    TXT
    user/pages/
    ├── 01.home
    │   └── default.md
    ├── 02.typography
    │   └── default.md
    └── 03.blog
      ├── blog.md
      └── item1
          └── item.md
    
  • Browsed to '/blog'
  • Entered a search term 'xxx'
  • Result was shown on page /search/query:xxx

No idea what could have caused the issue on your site... Maybe it has to do with theme Gantry 5 Helium?

Would you mind setting up a fresh install of Grav 1.7 + Gantry 5 Helium + simplesearch + above folder structure?

5 years ago

@pamtbaau , but a home page alias points to home?

Probably something could be achieved with a multiple search feature, I've added this into blog.md:

YAML
simplesearch:
    process: true
    route: /grav-blog-page

But with this configuration, clicking on a last pagination page, brings me to a search result page instead of page:3... Also in doesn't give me a right base url in a blog item page

5 years ago

@01K, I tried to reproduce your issue using Gantry 5 Helium, but can't....

I did the following:

  • Fresh install of Grav 1.7.0-rc.20
  • Installed theme G5_Helium: $ bin/gpm install g5_helium
  • Installed Gantry5 Framework: $ bin/gpm install gantry5
  • Installed simplesearch: $ bin/gpm install simplesearch
  • Copied {% include 'partials/simplesearch_searchbox.html.twig' %} into line 6 of 'user/plugins/gantry5/engines/nucleus/templates/blog_list.html.twig'
  • Created a blog folder:
    TXT
    user/pages/
    ├── 01.home
    │   └── default.md
    ├── 02.typography
    │   └── default.md
    └── 03.blog
      ├── blog_list.md
      └── item1
          └── blog_item.md
    
  • Browsed to '/blog'
  • Entered a search term 'xxx'
  • Result was shown on page /search/query:xxx
5 years ago

@pamtbaau:
typography

But could you set a typography as a default home page and recheck the search?

5 years ago

@01K, You may do so if you like...

5 years ago

I meant, if you change a home page from a blog to other one this bug should appear.
I've found a old forum thread with something similar, still examining it

5 years ago

@01K, I have presented 2 reproducible tests showing SimpleSearch is working as expected.

Would you mind providing a similar step-by-step test case reproducing the issue you are experiencing?

5 years ago

@pamtbaau the quickest way to reproduce is to download a skeleton with prepended data and blog template. Because every blog template has base_url settings (set base_url).

As for example https://demo.getgrav.org/pinpress-skeleton/PinPress skeleton

Out of a box everything works, but once you change the homepage from predefined /blog - to /contact or any other page the issue will be represented

5 years ago Solution

@01K, With all due respect, I'm getting the impression you did not try the test scripts I provided... I apologise in case I'm mistaken...

In the test cases I've shown, the variable system.home.alias is by default /home not /blog and simplesearch plugin works fine.

I suspect however, it is a theme issue... Try the scripts from post 7 and install the 5 year old theme 'pinpress' and see what happens.

👍 1
5 years ago

Yes, with your provided script everything works fine.
It seems, that you are right, that the problem is in a old template.
I will compare all the twig logic in templates

5 years ago

@pamtbaau you were right!
The logic of template is outdated. I had to make slight adjustments and it seems now to work as it should :)

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 104 18 hours ago
General · by pamtbaau, 23 hours ago
1 88 23 hours ago
General · by Andy Miller, 1 day ago
0 53 1 day ago
General · by Marcel, 12 months ago
6 381 5 days ago
General · by Duc , 6 days ago
3 69 6 days ago