Chapters
43 Chapters
|
5:00:38
|
This course is archived!
While the concepts of this course are still largely applicable, it's built using an older version of Symfony (4) and React (16).
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.2.0
Subscribe to download the code!Compatible PHP versions: ^7.2.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
27.
API Setup & AJAX with fetch()
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
While the concepts of this course are still largely applicable, it's built using an older version of Symfony (4) and React (16).
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.2.0",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // v1.8.0
"doctrine/doctrine-bundle": "^1.6", // 1.9.1
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.3
"doctrine/doctrine-fixtures-bundle": "~3.0", // 3.0.2
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.3.1
"doctrine/orm": "^2.5", // v2.7.2
"friendsofsymfony/jsrouting-bundle": "^2.2", // 2.2.0
"friendsofsymfony/user-bundle": "dev-master#4125505ba6eba82ddf944378a3d636081c06da0c", // dev-master
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.0
"symfony/asset": "^4.0", // v4.1.4
"symfony/cache": "^3.3|^4.0", // v4.1.4
"symfony/console": "^4.0", // v4.1.4
"symfony/flex": "^1.0", // v1.21.6
"symfony/form": "^4.0", // v4.1.4
"symfony/framework-bundle": "^4.0", // v4.1.4
"symfony/lts": "^4@dev", // dev-master
"symfony/monolog-bundle": "^3.1", // v3.3.0
"symfony/polyfill-apcu": "^1.0", // v1.9.0
"symfony/property-access": "^3.3|^4.0", // v4.1.4
"symfony/property-info": "^3.3|^4.0", // v4.1.4
"symfony/serializer": "^3.3|^4.0", // v4.1.4
"symfony/swiftmailer-bundle": "^3.1", // v3.2.3
"symfony/twig-bundle": "^4.0", // v4.1.4
"symfony/validator": "^4.0", // v4.1.4
"symfony/yaml": "^4.0", // v4.1.4
"twig/twig": "2.10.*" // v2.10.0
},
"require-dev": {
"easycorp/easy-log-handler": "^1.0.7", // v1.0.7
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.4
"symfony/dotenv": "^4.0", // v4.1.4
"symfony/maker-bundle": "^1.5", // v1.5.0
"symfony/phpunit-bridge": "^4.0", // v4.1.4
"symfony/stopwatch": "^3.3|^4.0", // v4.1.4
"symfony/var-dumper": "^3.3|^4.0", // v4.1.4
"symfony/web-profiler-bundle": "^3.3|^4.0", // v4.1.4
"symfony/web-server-bundle": "^4.0" // v4.1.4
}
}
What JavaScript libraries does this tutorial use?
// package.json
{
"dependencies": {
"@babel/plugin-proposal-object-rest-spread": "^7.12.1" // 7.12.1
},
"devDependencies": {
"@babel/preset-react": "^7.0.0", // 7.12.5
"@symfony/webpack-encore": "^0.26.0", // 0.26.0
"babel-plugin-transform-object-rest-spread": "^6.26.0", // 6.26.0
"babel-plugin-transform-react-remove-prop-types": "^0.4.13", // 0.4.13
"bootstrap": "3", // 3.3.7
"copy-webpack-plugin": "^4.4.1", // 4.5.1
"core-js": "2", // 1.2.7
"eslint": "^4.19.1", // 4.19.1
"eslint-plugin-react": "^7.8.2", // 7.8.2
"font-awesome": "4", // 4.7.0
"jquery": "^3.3.1", // 3.3.1
"promise-polyfill": "^8.0.0", // 8.0.0
"prop-types": "^15.6.1", // 15.6.1
"react": "^16.3.2", // 16.4.0
"react-dom": "^16.3.2", // 16.4.0
"sass": "^1.29.0", // 1.29.0
"sass-loader": "^7.0.0", // 7.3.1
"sweetalert2": "^7.11.0", // 7.22.0
"uuid": "^3.2.1", // 3.4.0
"webpack-notifier": "^1.5.1", // 1.6.0
"whatwg-fetch": "^2.0.4" // 2.0.4
}
}
9 Comments
What could be the reason for Web Debug Toolbar not showing
fetch()calls from a React component, but when I try exactly the same call from e.g. browser's console – it shows just fine?Hey @ameotoko
That's weird, the "debug toolbar" should catch all AJAX requests. Does it happen only with
fetch(), what if you useaxiosorjQueryinstead? (I'm not saying that you should switch libraries, this is only for debugging purposes)Cheers!
Didn't work with
jQueryeither... but then I had a hunch – I added a timeout of 3 seconds before callingfetch()orjQuery.ajax(). I just wanted to absolutely make sure I can see the WDT before AJAX call is made... And sure enough – my AJAX calls are now being properly shown, with eitherfetch()orjQuery!So the problem is – my frontend code is simply executed too fast, way before Symfony's javascript has enough time to load and initialize. I never thought I'd have to deal with THAT kind of problem ever 🤔
This is also annoying because I'm not sure what I can do... The production code is obviously fine. So I need to somehow check for
devenvironment or WDT being present, and do it from a React component? No idea...Hey @Andrey!
Ha! That's good debugging!
Yea, it's tricky. The WDT is loaded via Ajax itself... so indeed, it'll take a few hundred milliseconds before it registers its code to listen to the Ajax calls. If you care enough and are using Encore, I think you could use
process.env.NODE_ENVin your code in an if statement. Iirc, we rewrite that to true/false based on if you're doing an encore dev or production build. That might be good enough.Cheers!
Hi, friends, I'd need some help.
Instead of
RepLogsI serveAgents, but no matter. All the same following the tutorial.# I've a problem with the routing in the
fetch()callMy "list of agents" API lives here
/api/agentsthanks to this route:The controller that paints the twig where the root component
<AgentApp />is mounted is served here:/private/admin/react-testin a routing like this:Okey, when I need routes in the twig, I easily use
path( 'api_agent_list' )so the URL is represented correctly.But in this chapter the
rep_log_api.js(in my caseagent_api.js) does a fetch directly to the URL, in the example i the video to/reps.Thing is...
I do not serve my project from the root, but from a directory, and the project is not aware of the directory it's served from.
In fact, my controller in
private/admin/react-testis served specifically from herehttp://127.0.0.1:22080/repos/hello-trip/telethon-parsec_agent-panel/public/private/admin/react-testThis yields in a routing problem:
fetch( '/api/agents' )with/then the API is loading fromhttp://127.0.0.1:22080/api/agentswhich does not exist.fetch( 'api/agents' )without/then the API is loading relative to the base of the controller path, soi it's trying to fetch from here:http://127.0.0.1:22080/repos/hello-trip/telethon-parsec_agent-panel/public/private/admin/api/agentswhich is also incorrect.In twig we play with the routing name, in this case
api_agent_list, so the symfony framework "builds" the proper route. Worth mentioning that when I configure webpack I need to change this.setPublicPath( '/build' )into this.setPublicPath( 'build' )so subdirectory access is allowed.It works... in twig.
# Question
What do I have to put inside the
fetch()in the javascript so it loads the proper path given in the route and supporting subdirectory deploying?Hey Xmontero,
If you want to use the correct URLs in your JS files - the best way is to generate the code with
path()orurl()Twig functions, i.e. generate URLs in Twig templates and pass them into the JS code. This way you will be sure that URLs are generated correctly. You can pass pre-geenrated URLs in a few ways, e.g. setting a data attribute on a tag an then read it from the JS code, or setting a global JS variable in script HTML tag in a Twig template, etc.I hope this helps!
Cheers!
Tested, it works!
Just in case it helps anyone, what I did is:
<meta>tag in my header with the API base path.My javascript looked like this initially (hardcoded path, bad code):
To overcome this I've done those editions:
In the routing I added this non-callable route:
In the base HTML template I added a meta tag before the javascripts, in the header:
In the javascript I get the data attribute as explained here: https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes
I hope this code helps someone serving from a subdir! Thanks @victor for the tip.
Hey Xmontero,
Awesome! Thanks for confirming it worked for you, and thanks for the details examples that could be useful to others!
Cheers!
I see! Very good idea! I'll try it!
"Houston: no signs of life"
Start the conversation!