Multiple controllers based on my-first-app project?

Enonic version: 6.14.3
OS: Ubuntu 16.04

I am trying to build a demo for my team to evaluate whether Enonic meets our needs, based on the my-first-app tutorial. This is my first time working with Enonic and Javascript-based Thymeleaf controllers, and there is some time pressure so I apologize for my naive questions.

The my-first-app shows how to create one controller. How can I add additional controllers to this project? Do I add a ‘site’ for each controller? I found the instructions at readthedocs /en/stable/developer/projects/structure.html for adding additional controllers to site.xml. Is that the right way to add additional controllers?

Hi!
If you want to start building apps for sites, I recommend looking into this guide: https://developer.enonic.com/guides/my-first-site

NB! The developer portal as a whole is “under construction” and will be completed after summer 2018

There are many types of http controllers There are page controllers,
part controllers and layout controllers.

Each page controller must reside in its own folder under the site/pages, and part controllers sits below the site/parts directory, and layouts sits below the site/layouts folder. The name of the controller JavaScript file must be the same as the directory that contains it.

Services also use controllers.

Controllers only have to be registered in site.xml to allow the creation of HTTP endpoints that are bound to a combination of a URL pattern and/or content property. These are called controller mappings. Http endpoints can also be set up using the Application controller (main.js) and the router library from Enonic Market.

I also recommend looking at the project structure for an application.

1 Like

This appears to be identical to the “my first app” guide I mentioned in the original post. My difficulty comes when I create a new controller, with the same structure as the “hello” controller in the tutorial, with a new folder, js file, and html file . Where do I hook it up to the application? If I use the GUI, the tutorial seems to imply that I create another “site” but that seems like overkill for a new controller.

A related question, is there an easy way to see what the final URL will be of the controllers I create? I see there is a preview, but how do I know where this page is supposed to show up? I read to the end of the tutorial and found that I should be looking in /portal/master, but is there a way to look up this location in the app itself?

My mistake, I thought you had completed this guide: https://developer.enonic.com/guides/hello-xp

If you have created another controller in the page folder, these controllers can be “instanciated” by any content. Simply go to your existing site in Content Studio, create a new content item, click on the “screen icon” top right, and choose your new controller.

As such, the controllers are not mapped to a fixed url, but rather driven by your content structure.
If you want to map a controller to a specific path i.e. mysite/mycontroller, you can use mappings in your site.xml as defined here: http://xp.readthedocs.io/en/stable/developer/site/mappings/

In terms of “final url”. If your site is named “mysite/” and placed on the root in Content Studio, the url will be /portal/master/mysite - all other pages (and controller mappings) will be within this url.

NB! For a site to be live on /portal/master it has to be published, the drafts are available on /portal/draft. To go completely “live” with a site you would also configure a vhost definition i.e. mydomain.com -> /portal/master/mysite. Then, all traffic going to mydomain.com will show your site.

Docs for vhosts is here: http://xp.readthedocs.io/en/stable/operations/configuration.html#virtual-host-configuration

As you can see, a site can have many “urls” as we progress - but luckily you don’t need to worry about this when creating links in your code. By using portalFunctions you can create urls that will always be correct - regardless of your context.

Hope this helps :slight_smile:

3 Likes