webpack encore fetch in symfony 5

I’m learning to use webpack Encore

I would like to know if it is possible to use the ‘path’ function to call a route via ajax

webpack.config.js

    .setOutputPath ('public / build /')
    .setPublicPath ('/ build')
    .addEntry ('app', './assets/js/app.js') 
    .addEntry ('ajax', './assets/js/ajax.js')

I created a second entry which imports all ajax calls

ajax.js

import 'bootstrap'; // adds functions to jQuery

import articleShow './myAjax/articleShow';
articleShow();
...

myAjax > articleShow.js

is it possible to use the path function ?

        ...
        let formData = new FormData();

        formData.append("folder", folder);
        formData.append("id", id);

        **var urlAjax = "{{ path('showArticle') }}";** (pb)!!!
        //var urlAjax = "https://localhost:8000/article/show";
        fetch(urlAjax, {
            method: 'POST',
            body: formData
        })
            .then(function (response) {
                return response.json();
            })
            .then(function (message) {
            ...

Articlecontroller

    /**
    * @Route("/article/show", name="showArticle")
    */
    public function showArticle(....) {
     ...

question 1: Is it possible to use the ‘path’ function with webpack encore?

question 2: is my approach correct? if no, do you have a suggestion with a concrete example because i have hardly found anything as documentation.

thanks for your help.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *