Custom Component: PHP includes

I am starting to work on turning my CMS blog into components. :slightly_smiling_face:

What is the best way to handle custom components in a php pages that include separate php code stored in a folder.

For example in my forthcoming CMS blog, there will be a number of components that typically be added to their own page e.g. list blog posts, view blog posts, edit blog posts etc.

Currently my PHP code includes routines store in a common ‘handlers’ folder at the root level; routines such as retrieve metadata, autosave, upload image, delete post, save post etc. The PHP code in the components will include these routines e.g. ‘require_once “handlers/metadata.php”;’

It is also likely that I would like to have a common component that would likely be in a global, that would be setup to initialise the CMS blog environment?

This maybe what your looking for:

Possibly? The documentation doesn’t explain how shared files get transported to a published site and how you would reference them in your PHP code.

I guess I could try it and see what happens. :wink:

@dan Looking at the documentation, it isn’t clear where I would put a number of php files that get included in my php code in a component.

Here is an example of my code:

@portal(pageStart, id: "com.logrunner.blog", includeOnce: true)

<?php
// Version: 1.4.0 Logrunner Blog © autolog [2025]
session_start();

require_once "handlers/metadata.php";
require_once "handlers/check_md_filename.php";
...
1 Like

I think you’re after the “Backend” feature, have a read of this and let me know how you get on Backend | RapidWeaver Elements Docs

1 Like

Hi @tpbradley,
I have tried this out and if you store it the component’s templates folder with the component, it is being stored in the backend folder.

However, AFAICS, if you try and install a PHP included file in the DevPack’s ‘shared/templates/backend’ it isn’t getting saved in the e.g. ‘backend/rw090101E2_026E_4DE0_A3F4_46606F517DF6’ folder.

A number of my included PHP files are used in multiple components.

@tpbradley

To add to the above; if I add a folder to the ‘backend’ folder, with some PHP files in, when I publish the Elements project, there are no error messages produced (that I can detect) none of the pageStart DevPack PHP code is included in the published index.php file, although the HTML code is. The DevPack debug messages has nothing in it.

So it was only by interrogating the published index.php file that I could see that the PHP code wasn’t included. :thinking:

Moving the sub-folders PHP files, up a level, into the backend folder and removing the sub-folder resolves the issue

It would be useful (though not a show stopper) to have sub-folders as it would help to keep things more organised. :slight_smile:

I’ve been able to reproduce this here - add a subdirectory to the backend directory and everything within backend fails to publish. I’ve added a warning to the docs about this, along with a ticket to add support for subdirectories.

Cheers!

1 Like

Actually, having chatted internally about it, I had forgotten we hit issues trying to do this before. However, we came up with a solution using a combination of files in backend and in shared/assets.

There’s more information here Backend | RapidWeaver Elements Docs

Essentially, you’d store your library code (stuff that doesn’t change) in the shared/assets directory. Then include it from your backend files, calling the functions with the component properties.

Hope this makes sense?

I can’t seem to get this to work? :confused:

My hooks.js is set like this:

rw.setProps({
    siteAssetPath: rw.component.siteAssetPath,
    node: rw.node,
    ...

My PHP code is like this:

$backend_path = "{{node.backendPath}}";  // ELEMENTS SUBSTITUTION
$site_asset_path = "{{siteAssetPath}}";  // ELEMENTS SUBSTITUTION
error_log("\$backend_path: " . $backend_path);
error_log("\$site_asset_path: " . $site_asset_path);

This is what I see in the PHP log:

[25-Mar-2025 16:44:13 UTC] $backend_path: ./backend/rw30317FDF_D121_49E8_8454_C12FAA9DB759/
[25-Mar-2025 16:44:13 UTC] $site_asset_path: rw/elements

My DevPack structure looks like this:

My published (MAMP Pro) site structure looks like this:

I can’t see any assets in the published site?

It’s probably something I have setup incorrectly. :wink:

It’s a little tricky to see but I’m fairly certain the shared directory in the dev pack just needs to be moved into the components directory, so you should have

components
    shared
        assets
            test.php

Hopefully that’ll do the trick!

Thanks - Yes, that was it - well spotted. :slightly_smiling_face:

1 Like