|
| 1 | +<!-- |
| 2 | +{ |
| 3 | + "title": "Component Mappings", |
| 4 | + "id": "mappings-component-mappings", |
| 5 | + "related": [ |
| 6 | + "mappings-how-to-define-a-reg-mapping", |
| 7 | + "tag-application", |
| 8 | + "function-expandpath", |
| 9 | + "cookbook-application-context-set-mapping" |
| 10 | + ], |
| 11 | + "categories": [ |
| 12 | + "application", |
| 13 | + "components", |
| 14 | + "server" |
| 15 | + ], |
| 16 | + "description": "How to define and use component mappings in Lucee.", |
| 17 | + "keywords": [ |
| 18 | + "Component Mapping", |
| 19 | + "Classpath", |
| 20 | + "CFCs", |
| 21 | + "Application.cfc", |
| 22 | + "Lucee archive" |
| 23 | + ] |
| 24 | +} |
| 25 | +--> |
| 26 | + |
| 27 | +# Component Mappings |
| 28 | + |
| 29 | +Component mappings in Lucee define locations where the server will look for CFML components (CFC files) when using the `createObject()` function or the `new` operator. They provide a way to organize and access your components without having to specify the full path each time. |
| 30 | + |
| 31 | +## Understanding Component Mappings |
| 32 | + |
| 33 | +Component mappings provide a way to organize and access your CFC files without having to specify the full path each time. This is particularly useful in large applications with multiple component packages. |
| 34 | + |
| 35 | +**Important**: Component mappings point to the root of package paths, not to individual components. For example, if you have a mapping pointing to: |
| 36 | + |
| 37 | +``` |
| 38 | +/path/to/lucee-server/context/components/ |
| 39 | +``` |
| 40 | + |
| 41 | +And a component at: |
| 42 | + |
| 43 | +``` |
| 44 | +/path/to/lucee-server/context/components/org/lucee/extension/quartz/ComponentJob.cfc |
| 45 | +``` |
| 46 | + |
| 47 | +You can reference it in your code using: |
| 48 | + |
| 49 | +```coldfusion |
| 50 | +// Using fully qualified path with the new operator |
| 51 | +new org.lucee.extension.quartz.ComponentJob() |
| 52 | +
|
| 53 | +// Or using import and then the short name |
| 54 | +import org.lucee.extension.quartz.ComponentJob; |
| 55 | +new ComponentJob(); |
| 56 | +``` |
| 57 | + |
| 58 | +Like other mapping types in Lucee (regular mappings and custom tag mappings), component mappings consist of several parts: |
| 59 | + |
| 60 | +* **physical**: Physical directory for the root of the mapping (packages start inside that directory) |
| 61 | +* **archive**: A Lucee archive (.lar file) that contains the components; a .lar file is the same as a .jar file in Java, containing the compiled component (and optionally the source) |
| 62 | +* **primary**: When both "physical" and "archive" are defined, this setting determines where to look first for a component; by default, it looks first in "physical"; possible values are "physical" and "archive" |
| 63 | +* **readonly**: Determines if the mapping can be configured in the Lucee admin or not (not needed for mappings defined in Application.cfc) |
| 64 | +* **hidden**: Controls visibility in the Lucee admin (not needed for mappings defined in Application.cfc) |
| 65 | +* **inspectTemplate**: Controls Lucee's behavior when checking for changes |
| 66 | + |
| 67 | +## Defining Component Mappings |
| 68 | + |
| 69 | +### In the Lucee Administrator |
| 70 | + |
| 71 | +Component mappings can be defined in the Lucee Server or Web Administrator. Go to the "Archives & Resources/Component" page. |
| 72 | + |
| 73 | +Mappings defined in the Server Administrator are visible to all web contexts, while mappings defined in the Web Administrator are only visible to the current web context. |
| 74 | + |
| 75 | +### Using CFConfig |
| 76 | + |
| 77 | +Component mappings can be defined in a CFConfig JSON file: |
| 78 | + |
| 79 | +```json |
| 80 | +{ |
| 81 | + "componentMappings": [ |
| 82 | + { |
| 83 | + "physical": "{lucee-config}/components/", |
| 84 | + "archive": "", |
| 85 | + "primary": "physical", |
| 86 | + "inspectTemplate": "always" |
| 87 | + } |
| 88 | + ] |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +This is the default component mapping that Lucee always creates, but you can define as many mappings as needed. |
| 93 | + |
| 94 | +### In Application.cfc |
| 95 | + |
| 96 | +Component mappings can also be defined in the Application.cfc file, making them specific to the current application: |
| 97 | + |
| 98 | +```cfs |
| 99 | +// Application.cfc |
| 100 | +component { |
| 101 | + this.componentpaths = [ |
| 102 | + { |
| 103 | + "physical": "{lucee-config}/components/", |
| 104 | + "archive": "", |
| 105 | + "primary": "physical", |
| 106 | + "inspectTemplate": "always" |
| 107 | + } |
| 108 | + ]; |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Using Component Mappings |
| 113 | + |
| 114 | +Once you've defined your component mappings, you can create components without specifying the full path: |
| 115 | + |
| 116 | +```coldfusion |
| 117 | +// If you have a component mapping that includes a "models" directory |
| 118 | +component = createObject("component", "models.User"); |
| 119 | +
|
| 120 | +// Or using the new operator |
| 121 | +component = new models.User(); |
| 122 | +``` |
| 123 | + |
| 124 | +## Advanced Usage |
| 125 | + |
| 126 | +### Using Archives |
| 127 | + |
| 128 | +You can package your components into a Lucee Archive (.lar) file and reference it in your component mapping: |
| 129 | + |
| 130 | +```cfs |
| 131 | +// Application.cfc |
| 132 | +component { |
| 133 | + this.componentpaths = [ |
| 134 | + { |
| 135 | + physical: getDirectoryFromPath(getCurrentTemplatePath()) & 'components', |
| 136 | + archive: getDirectoryFromPath(getCurrentTemplatePath()) & 'components.lar', |
| 137 | + primary: 'archive' |
| 138 | + } |
| 139 | + ]; |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +With `primary` set to "archive", Lucee first checks the .lar file for components. If not found there, it looks in the physical path. |
| 144 | + |
| 145 | +### InspectTemplate Options |
| 146 | + |
| 147 | +The `inspectTemplate` attribute controls how Lucee checks for changes to your components: |
| 148 | + |
| 149 | +* **never**: Never checks for changes |
| 150 | +* **once**: Checks once per request |
| 151 | +* **always**: Always checks for changes (can impact performance) |
| 152 | + |
| 153 | +```cfs |
| 154 | +// Application.cfc |
| 155 | +component { |
| 156 | + this.componentpaths = [ |
| 157 | + { |
| 158 | + physical: getDirectoryFromPath(getCurrentTemplatePath()) & 'components', |
| 159 | + inspectTemplate: 'once' |
| 160 | + } |
| 161 | + ]; |
| 162 | +} |
| 163 | +``` |
0 commit comments