# Custom Component: PHP includes

**URL:** <https://forums.realmacsoftware.com/t/custom-component-php-includes/46746>\
**Category:** Elements API\
**Created:** [March 15, 2025, 11:23am UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746 "2025-03-15T11:23:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 15, 2025, 11:23am UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/1 "2025-03-15T11:23:31Z")

</div>

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

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?

---

<div class="post-metadata">

**Author:** ![Doobox](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/doobox/32/32373_2.png) [@Doobox](https://forums.realmacsoftware.com/u/Doobox)\
**Post date:** [March 16, 2025, 10:57pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/2 "2025-03-16T22:57:17Z")

</div>

This maybe what your looking for:

> **[Shared Files | RapidWeaver Elements Docs](https://docs.realmacsoftware.com/elements-docs/elements-language/bundle-structure/shared-files)**

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 17, 2025, 9:48am UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/3 "2025-03-17T09:48:52Z")

</div>

> [@Doobox](#):
>
> 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. 😉

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 17, 2025, 3:58pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/4 "2025-03-17T15:58:54Z")

</div>

> [@logrunner](#):
>
> I guess I could try it and see what happens. 😉

@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:

```auto
@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";
...

```

---

<div class="post-metadata">

**Author:** ![tpbradley](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/tpbradley/32/25503_2.png) [@tpbradley](https://forums.realmacsoftware.com/u/tpbradley)\
**Post date:** [March 20, 2025, 11:31am UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/5 "2025-03-20T11:31:09Z")

</div>

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](https://docs.realmacsoftware.com/elements-docs/elements-language/bundle-structure/components/templates/backend)

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 20, 2025, 12:22pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/6 "2025-03-20T12:22:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 20, 2025, 2:28pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/7 "2025-03-20T14:28:27Z")

</div>

@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. 🤔

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. 🙂

---

<div class="post-metadata">

**Author:** ![tpbradley](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/tpbradley/32/25503_2.png) [@tpbradley](https://forums.realmacsoftware.com/u/tpbradley)\
**Post date:** [March 24, 2025, 2:16pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/8 "2025-03-24T14:16:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![tpbradley](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/tpbradley/32/25503_2.png) [@tpbradley](https://forums.realmacsoftware.com/u/tpbradley)\
**Post date:** [March 24, 2025, 10:19pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/9 "2025-03-24T22:19:33Z")

</div>

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](https://docs.realmacsoftware.com/elements-docs/elements-language/bundle-structure/components/templates/backend#subdirectories-are-not-supported)

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?

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 25, 2025, 5:14pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/10 "2025-03-25T17:14:08Z")

</div>

I can’t seem to get this to work? 😕

My hooks.js is set like this:

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

```

My PHP code is like this:

```auto
$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:

```auto
[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:

 ![CleanShot 2025-03-25 at 16.38.37](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/0/a/0a19f6d1b70d54335576ed2a9cd28b142fac5cfc.png)

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

 ![CleanShot 2025-03-25 at 16.46.50](https://europe1.discourse-cdn.com/flex005/uploads/realmacsoftware1/original/3X/d/0/d0376b954712c18c3b664fa83fd9d1dee18fbd73.png)

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

It’s probably something I have setup incorrectly. 😉

---

<div class="post-metadata">

**Author:** ![tpbradley](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/tpbradley/32/25503_2.png) [@tpbradley](https://forums.realmacsoftware.com/u/tpbradley)\
**Post date:** [March 25, 2025, 7:25pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/11 "2025-03-25T19:25:58Z")

</div>

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

```auto
components
    shared
        assets
            test.php

```

Hopefully that’ll do the trick!

---

<div class="post-metadata">

**Author:** ![logrunner](https://avatars.discourse-cdn.com/v4/letter/l/59ef9b/32.png) [@logrunner](https://forums.realmacsoftware.com/u/logrunner)\
**Post date:** [March 25, 2025, 9:07pm UTC](https://forums.realmacsoftware.com/t/custom-component-php-includes/46746/12 "2025-03-25T21:07:18Z")

</div>

> [@tpbradley](#):
>
> Hopefully that’ll do the trick!

Thanks - Yes, that was it - well spotted. 🙂
