Posts AMP Integration
Created 6 years ago by mitch

I'm trying to add in AMP compatibility for Posts. I've got the routing working with my module that routes posts/{slug}/amp to a controller extending the core PostsController, with a replacement view() method that parses the response from MakePostResponse. I override the posts.view and it successfully only overrides when the url has the /amp at the end.

However, this all breaks when I try to override the default posts view.twig with a theme file. If I add in view.twig in the theme to override the default posts view.twig, every posts uses that new twig, whether /amp or not. Is there a way to use $overrides inside my module to override a theme view with the amp one inside my module? Should I just break it down in the controller?

I'd like to not have to clone the posts module to get this done, but is that the most reasonable way to go about it? I'd love some direction. This is my first project with PyroCMS, and it's been a while since I've worked in PHP, so I'm still getting my bearings.

I'd also like some clarification on which overrides take priority. is very helpful, but which override takes precedence?

Thanks so much, I'm stoked to find a Laravel CMS that makes sense.

ryanthompson  —  6 years ago

Welcome @mitch! We're glad to have you 😊

Can you post the code for your AMP controller? Here is the code responsible for the overriding (there is a view composer that does the same - but separated for whatever reason.. can't recall but logic is the same):

I really doubt cloning the Posts module will be the best thing. Because everything is extendable from the outside. BUT.. your situation may be different IDK. If you can show me some code to shed some light on your intentions I might be able to come up with something for you as well.

Why the AMP suffix? Are you trying to change the view on mobile devices only? If so there is an agent helper that might be of use to you. Or some callback magic or something. I am not familiar with AMP unfortunately.

ryanthompson  —  6 years ago

Based on my AMP crash course and some assumptions you really just need a way to override this view that's hardcoded here it would seem:

Pages have the handler extensions which basically do the work of MakePostResponse but for pages. Posts however do not have a handler though I could see delegating that logic to a class that could be overridden. Then you could take the reigns for everything.

Does that sound like something that would work?

mitch  —  6 years ago

Thanks for the quick reply! Thank you for sending that overload method my way.

Background: we're migrating a client site from Wordpress, and they have AMP integration. The blog isn't news/time sensitive, and the site's responsive, but we have strict order from their SEO guy to make sure it's AMPed up. We're using /amp at the end because that's how their current site is set up, and they want all urls to remain the same.

After looking over everything you sent me, the solution I came up with is to route /amp to my custom controller as I have it now, then use a copy of MakePostResponse.php and another for PostResponse.php to use a different view. So for /blog/{slug}, it will route to Posts::view, and /blog/{slug}/amp will route to MyCustomController::ampView, which then calls MakeAMPPostResponse($post) instead of MakePostResponse($post), which in turn uses AMPPostResponse instead of PostResponse.

It's all working now, does this seem reasonable?