[英]webpack encore fetch in symfony 5
I'm learning to use webpack Encore我正在学习使用 webpack Encore
I would like to know if it is possible to use the 'path' function to call a route via ajax我想知道是否可以使用'path'函数通过ajax调用路由
webpack.config.js 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 调用
ajax.js ajax.js
import 'bootstrap'; // adds functions to jQuery
import articleShow './myAjax/articleShow';
articleShow();
...
myAjax > articleShow.js myAjax > 文章Show.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?问题 1:是否可以在 webpack encore 中使用“路径”功能?
question 2: is my approach correct?问题2:我的方法正确吗? if no, do you have a suggestion with a concrete example because i have hardly found anything as documentation.
如果没有,您是否有具体示例的建议,因为我几乎没有找到任何文档。
thanks for your help.谢谢你的帮助。
Thank you I succeeded with webpack encore.谢谢,我成功使用 webpack encore。 To help :
帮助 :
Intall Fos jsrouting bundle安装 Fos jsrouting 包
step 1 : expose your route in your controller otherwise you have to modify the file: fos_js_routes.json in the public folder or rename the route
/**
* @Route("/country", **options={"expose"=true},** name="ajaxCountry")
**/
step 2 :
import Routing from '../../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
const routes = require('../../../public/js/fos_js_routes.json');
Routing.setRoutingData(routes);
var formData = new FormData();
var urlAjax = Routing.generate('yourRoute');
fetch(urlAjax, {
method: 'POST',
body: formData
})
.then(function (response) {
return response.json();
})
.then(function (message) {
...
step 3 : in your terminal
php bin/console fos:js-routing:dump --format=json --target=public/js/fos_js_routes.json
bye再见
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.