Backend
Deploy extra files to the backend.
Last updated
Was this helpful?
Deploy extra files to the backend.
Last updated
Was this helpful?
Files added to the backend directory are processed in the same context as other template files. However, instead of forming part of the page, they will be deployed as extra files to the page's backend directory during publish.
To ensure there are no conflicts with other components, all backend files are stored in a subdirectory corresponding to the current node id. For example:
The following file types are supported:
html
php
js
css
A good use-case for this would be to process a contact form. Create a new component and add the following code to the templates/backend/submit.php
file.
Then place this in the templates/form.html
frontend file.
When the submit button is pressed in the above form, the form sends all the form values to the {{node.backendPath}}/submit.php
file on the server. The node.backendPath
property will be replaced with the node's unique id.
To use the node.backendPath
property, we'll need to use the hooks.js file. Create a hooks.js file in your component and add this.
The assets are deployed only once after a component from the pack is added to the page. Use the hooks file to find the site assets path and pass it to the backend file.
We've found it good practice to keep the backend php files to a minimum and use them to build a minimal config object using the properties from the component. Then call a method included from a php file within site assets, passing in the config object.
Elements monitors every file in the backend directory for changes. This can cause problems when adding large php libraries with hundreds of files. A better solution is to add the php library to the Element pack's directory.