1

I've developed an app which fetches data from an API based on Laravel 5.5. The marketing landingpage of the app is based on Craft CMS Version 3. The marketing website and the API and the databases of both systems are running on the same server.

I want to generate landingpages for each row of table X of the Laravel database.

www.website.com/awesome-landingpage-about-{slug}

What is the best approach to realize this?

  • I don't want to fetch the data directly from Laravel's database
  • I don't want to synchronize the Craft CMS database with the Laravel (add/remove the rows from the laravel's database as entries to Craft)
  • It would be awesome to be able to have an entry-type "Landingpage" where we can optionally create a landingpage, referencing to an ID of the laravel table and add additional content for the landingpages.

Would be a JSON-API from Laravel to Craft CMS Plugin a good performant idea?

One option would be to use a Dynamic Route and just fetch the data from 127.0.0.1 (because same server) from the template file? Or is there a smarter way in Craft CMS?

enter image description here

4

I'd make a template called landing that would have various entries/entry types in it where I can add whatever CMS content I want.

https://www.example.com/landing/landing-page-slug

For a nice content authoring experience, I'd make a custom Element that knows to go out over the wire and fetch the data from the Laravel database via whatever JSON endpoint you've set up in your Laravel app, and a custom Field that is just a simple ElementSelect for my custom Element.

That way in your Craft CMS entry, you could just click a button to browse the various rows in your remote Laravel db (presenting the information in a nice UI that'd look exactly like you're selecting another Craft entry).

Then on the templating side of things, it'd work like anything else does in Craft. Through the custom Element, you'd have access to all of the data from your remote Laravel db table, e.g.:

{{ entry.laravelData.someField }}

Bonus points would be awarded for a nice caching layer to keep things performant.

| improve this answer | |
  • Just an additional information to this great answer: if you don't want to store custom elements or additional records you could as well create a custom field that fetches the data in the normalizeValue function. Your field could contain a simple json object that contains information about how to speak to your JSON endpoint and when you populate it, fetch this data directly instead of creating an element. Or if lazy loading is important you populate the field with some kind of "query" like relation fields do to get it on demand – Robin Schambach Jul 14 '18 at 8:10
  • Thank you andrew! I think this is exactly what I am searching for. I understand the "concept" but absolutely have no idea how to start this in Craft. I develop a module with a Custom Element (docs.craftcms.com/v3/extend/element-types.html#getting-started)? Where would I add the functionality to fetch die JSON-data and parse it into Craft database? So in this case I would have to sync the both databases? – rakete Jul 14 '18 at 9:45
  • What about this approach? craftcms.stackexchange.com/questions/9600/… – rakete Jul 14 '18 at 9:49
  • You don't need to sync your database. You can fetch all necessary properties in your elements get functions. For example when you do element->getContent() you check if _content is null and if so access your JSON endpoint. Craft is not so strict like other CMS you have the freedom to do whatever you like with many build in functions, it's totally flexible – Robin Schambach Jul 14 '18 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.