Skip to content

Add basePath support to Image links and Latte templates#56

Draft
f3l1x wants to merge 4 commits intomasterfrom
claude/fix-image-storage-issue-23-ba6MK
Draft

Add basePath support to Image links and Latte templates#56
f3l1x wants to merge 4 commits intomasterfrom
claude/fix-image-storage-issue-23-ba6MK

Conversation

@f3l1x
Copy link
Copy Markdown
Member

@f3l1x f3l1x commented Mar 29, 2026

Summary

This PR adds support for including the application's base path in generated image links. This ensures that images are correctly referenced when the application is deployed in a subdirectory rather than at the root of the domain.

Key Changes

  • Image class: Added basePath parameter to constructor and updated createLink() method to prepend the base path to generated links
  • ImageStorage class:
    • Added optional IRequest parameter to constructor to extract base path from HTTP request
    • Added getBasePath() method that extracts the base path from the HTTP request (or returns empty string if no request provided)
    • Updated all Image instantiations to pass the base path
  • Latte extension: Removed manual base path concatenation from template macros since Image::createLink() now handles it automatically
  • DI extension: Updated to inject IRequest into ImageStorage constructor via dependency injection
  • Tests:
    • Added comprehensive BasePathIntegrationTest covering various base path scenarios (root, nested, with/without HTTP request)
    • Added LatteIntegrationTest verifying that Latte macros ({img}, {imgLink}, n:img) correctly include base paths in rendered output
    • Added unit tests to ImageTest for base path handling in Image::createLink()

Implementation Details

  • Base path is extracted from the HTTP request's path, removing the trailing index.php and trailing slash
  • When no HTTP request is provided, base path defaults to empty string
  • The base path is prepended to all image links, whether using friendly URLs or not
  • Latte macros now rely on Image::createLink() to include the base path, eliminating duplication

https://claude.ai/code/session_01FCGDD73ytpsRNwZkG4Xi5T

Add basePath support to Image::createLink() so that it returns
the full path including the application's base path. This eliminates
the need for users to manually prepend $basePath when using the method
outside of Latte templates.

- Add basePath property to Image class
- Add basePath parameter to ImageStorage constructor
- Add basePath configuration option to DI extension
- Update LatteExtension to use createLink() directly (basePath now included)
- Add test cases for basePath functionality
Instead of introducing a new configuration option, the basePath is now
automatically obtained from Nette's HTTP request service. This aligns
with how Nette applications typically work and requires no additional
configuration.

- Inject IRequest into ImageStorage via DI extension
- Add getBasePath() method that retrieves basePath from HTTP request
- Remove basePath configuration option (not needed)
Add comprehensive tests verifying:
- ImageStorage.getBasePath() returns correct value from HTTP request
- Image.createLink() includes basePath when set
- fromIdentifier() creates Image objects with correct basePath
- Backward compatibility when no HTTP request is provided
Add integration tests that render actual Latte templates and verify
the image storage macros correctly include basePath in output:

- testLatteImgTagIncludesBasePath: {img} tag with /my-app/ basePath
- testLatteImgTagWithRootBasePath: {img} tag with root basePath
- testLatteImgLinkIncludesBasePath: {imgLink} tag with nested basePath
- testLatteNImgAttributeIncludesBasePath: n:img attribute

Also adds php.ini for tests requiring tokenizer extension.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants