How to Prevent a request from being cached for LANGUAGE SWITCH routing Laravel Spatie responsecache

I am using Laravel Spatie response cache: https://github.com/spatie/laravel-responsecache and Multiple language (language switch..).

How to fix: Preventing a request from being cached for language switcher route in Laravel.

I tried to add: ->middleware(‘doNotCacheResponse’); in lang.switch route but without success…

Route::get('lang/{language}', ['as' => 'lang.switch', 'uses' => '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="662a070801130701032509081214090a0a03142615110f12050e2a070801">[email protected]</a>'])->middleware('doNotCacheResponse');

Expected results to prevent request when switching pages (links) between language to be cached….

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not sure if I understand you correctly. If the problem is that you get cached results after you select a different language, I think you should clear the entire cache:

LanguageController {

    public function switchLang() {
        // Switch language
        ResponseCache::clear();
    }

}

Also, seems like spatie/laravel-responsecache allows you to delete specific URIs with ResponseCache::forget('/some-uri');

To delete cache after altering your data, do something like this

PostController {

    public function index()
    {
        // Display list of posts
    }

    public function edit()
    {
        // Display form to edit post
    }

    public function update()
    {
        // Delete related cache here and update post
        ResponseCache::forget('/posts');
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x