Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Child Modules

Trongate v2 lets you nest modules inside other modules for perfect organization. No configuration. No ceremony. Just logical grouping that makes sense.

Drop a module inside another → it's instantly a child module.


The Structure

product_reviews is a child of shop. That's the entire setup.


URL Access

Access child modules via URLs using the hyphenated format:

How It Works:

Trongate sees shop-product_reviews and automatically looks in modules/shop/product_reviews/.

Underscores in URLs become hyphens in paths. It just works.

Accessing Child Modules From Code

From a Controller:

Method 1: Explicit Loading (Recommended)

Method 2: Hyphenated Property Syntax

Complete example:

Important Note: Child modules require explicit loading before use. The framework cannot auto-detect that a module is a child of the current module when accessed via simple property syntax ($this->child_module).

From a View:

Use Modules::run() with explicit data passing:

Remember: Trongate v2 requires explicit data passing. The second parameter can be any data type - strings, arrays, objects, integers, booleans, or null.

From a Model:

Models can also access modules:

For more information about how model files can load modules, check out the documentation on Loading Modules From Models.


Important Note: Remember that model files should only be used by their corresponding controller file. Other modules must not directly access another module's model file. For detailed information about model encapsulation and proper module interaction patterns, see the Model Files documentation.


Child Module Models

Name your child module model files like this:

Inside the child controller, the model is auto-available:


Critical: View Files in Child Modules

IMPORTANT - Common Pitfall: When a module becomes a child module, you must set view_module in your data array when calling $this->view().

The Problem:

Without view_module set, the framework looks for views in modules/child_module/views/ instead of modules/parent_module/child_module/views/.

The Solution:

Always include view_module in your data array when calling views from a child module controller:

Key Points:

  • Format: Use slash format: 'parent/child' (not hyphenated)
  • Location: Must be in the $data array passed to $this->view()
  • Consistency: Apply to ALL view() calls in the child controller
  • Error: Without this, you'll get "View not found" errors

Why This Happens: The framework uses the class name to guess the module location. For child modules, the class name (Product_reviews) doesn't match the actual path (shop/product_reviews).


Serving Assets from Child Modules

Trongate v2 Change: The old assets/ folder is gone.

Store files directly in subdirectories like css/, js/, images/, fonts/, or any other name.

For more information about how to store and work with files that are inside modules, refer to the Module Assets Documentation.

Directory Structure:

Serving the Assets:

Use the hyphenated module name with the _module trigger:

Complete Example:

Child module controller using proper template pattern with additional includes:

Example from Forums.php showing the same pattern:

Important Rules:

  • Assets belonging to a child module must be in a subdirectory (not directly in the parent module root)
  • Use sensible directory names (css/, js/, images/, etc.)
  • Never name a module with _module suffix
  • Assets are served securely - PHP files and sensitive files are blocked

When to Use Child Modules

✅ Perfect For
  • Reviews for products
  • Comments for blog posts
  • Items in an order
  • Settings for a user
❌ Avoid When
  • Feature is standalone
  • Used by multiple parents
  • Deep nesting needed
  • Temporary functionality

Best Practice: One level deep is ideal. If you're thinking "grandparent-parent-child," go for a long walk and reconsider your life choices.

Troubleshooting Child Modules

Common Issues and Solutions:

1. "View not found" Error

Symptom: Getting "View 'filename' not found" when accessing a child module.

Solution: Ensure $data['view_module'] = 'parent/child'; is set in ALL $this->view() calls.

2. Module Not Found in Controller

Symptom: "Module controller not found" when accessing $this->child_module.

Solution: Use explicit loading first: $this->module('parent-child_module'); or use hyphenated property syntax: $this->{'parent-child_module'}

3. Module Not Found with Modules::run()

Symptom: "Controller not found" error when using Modules::run().

Solution: Use hyphenated format: Modules::run('parent-child/method', $data)

4. Assets Not Loading

Symptom: CSS/JS/images from child module not appearing.

Solution: Use hyphenated format with _module suffix: parent-child_module/css/file.css

Quick Reference Table:

Context Format Example
URL Access Hyphenated /shop-product_reviews/display
Controller Access (Method 1) Explicit loading then property $this->module('shop-product_reviews'); $this->product_reviews->method()
Controller Access (Method 2) Hyphenated property syntax $this->{'shop-product_reviews'}->method()
Modules::run() Hyphenated Modules::run('shop-product_reviews/display', $data)
view_module in data Slash format $data['view_module'] = 'shop/product_reviews'
Asset URLs Hyphenated + _module shop-product_reviews_module/css/style.css

Migration Checklist: Converting to Child Module

When moving an existing module to become a child module, follow this checklist:

  1. Move Directory: Move module folder inside parent module
    modules/child_module/ → modules/parent_module/child_module/
  2. Update URLs: Change all URL references to hyphenated format
    child_module/method → parent-child_module/method
  3. Update Modules::run() calls: Use hyphenated format
    Modules::run('child_module/method', $data)
    → Modules::run('parent-child_module/method', $data)
  4. Add explicit module loading: Update controller code to load child modules explicitly
    // Before: $this->child_module->method()
    // After: 
    $this->module('parent-child_module');
    $this->child_module->method();
  5. Add view_module to ALL view calls: Most critical step!
    // In child controller methods:
    $data['view_module'] = 'parent/child_module'; // Slash format
    $this->view('view_file', $data);
  6. Update Asset URLs: Use hyphenated format with _module suffix
    child_module_module/css/style.css
    → parent-child_module_module/css/style.css
  7. Test Navigation: Verify all links work with new URL format
  8. Test Views: Ensure all views render correctly
  9. Test Assets: Confirm CSS, JS, images load properly

Pro Tip: Use search/replace in your code editor:

  • Find: 'child_module/ → Replace: 'parent-child_module/
  • Find: anchor('child_module/ → Replace: anchor('parent-child_module/
  • Find: Modules::run('child_module/ → Replace: Modules::run('parent-child_module/

Bottom Line

Child modules give you logical organization without complexity. Trongate v2 provides multiple ways to access child modules, each suited for different scenarios.

Golden Rules for Child Modules:

  1. Always set view_module in your data array when calling views from child modules
  2. Use explicit $this->module('parent-child_module') before accessing child modules via property syntax
  3. For one-off access, use hyphenated property syntax: $this->{'parent-child_module'}
  4. For view integration, use Modules::run('parent-child_module/method', $data)

With these patterns, you can organize your code logically while maintaining clean, maintainable architecture.