08.
Adding Links via Annotations
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.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
10 Comments
Hey guys,
Great content as always, this course has helped me immensely!
In my app I have 2 directories for controllers:
/web - for all routes for the web app i.e. app/posts retrieves the html and javascript templates for the post page template
/api - all api routes - for examples api/posts returns the JSON data for all posts
So with regards to links, I have a problem. If I want to link to a post then the web link (/app/posts/123) works great. However, if I want to edit a post then this is done via an AJAX call, and the route is different (PATCH: /api/posts/123).
Is it possible that the links annotation can be updated to have a web and an api link? Is this good practice, or is there a better way to do this?
Hey Shaun T.!
You can make your controller's action to serve 2 different routes by specifying another "@Route" annotation, but I don't think is a good idea for your case. What you can do is to use the "edit" API endpoint in your web AJAX calls
Cheers!
This tutorial really opened up the world of annotations for me. But, should I worry about caching? Does the doctrine annotation reader automatically cache the annotations for me?
Yo bblue
Great question! The answer is, yes! Well, actually, the answer is: "it depends", but in this case yes. So, the Doctrine library exposes some classes that help read annotations. If you use them directly, it's possible to read annotations and *not* cache them. But, when you're in Symfony, Symfony has pre-configured the annotation_reader service to cache things, and all systems that read annotations use this service. So, in Symfony, yep, it's all cached. It may depend on your Symfony version, but in our app, these are cached via the filesystem to one of the directories in var/cache/dev/pools.
Cheers!
Hi Ryan,
In my case, forward slashes in
_linksare escaped with a backslash:.
Any idea why this might be happening?
Thank you!
Found a solution: https://github.com/schmittjoh/JMSSerializerBundle/issues/289
added the following options to config.yml:
Well, it works when I run it in Terminal, but not in the browser.
Also tried (to no avail):
Hey Vlad!
Setting these options works when you run it in your terminal (do you mean, like running unit tests?) but not in the browser? That by itself looks weird to me! Tell me more about that. From what I can see, you're doing the right types of things - the JsonSerializationVisitor uses those options in json_encode(). I'd add some temporary breakpoints or dump code in that class to see if your options are making it there.
Btw - what's wrong with the slash escaping? Just looking kinda ugly (it is a bit ugly)?
Cheers!
Hi Ryan,
Yes, it works when I run unit tests, but not when I load it in the browser.
I'll follow your suggestion to set breakpoints or dump() to see what's going on.
Do you think there might be a PHP ini setting that does that? I noticed, you don't get slashes escaped.
Nothing wrong in the slash escaping, it is just not required by JSON. But if I can't figure it, I'll leave it as is. No big deal.
Thank you!
Hi Vlad!
Wow, that *is* weird! I don't initially see any php.ini setting for this (but I have never really looked into this too far), but it could still be the cause. Or, something more subtle is happening!
And btw, at the end of this video when I load the JSON in the browser, I DO also have the escaped slashes. My JsonView Chrome extension is cleaning this up for me - if you look at the actual dumped source, the slashes are escaped.
If you end up figuring it out - let me know, I'm also curious :).
Cheers!
"Houston: no signs of life"
Start the conversation!