Add basePath support to Image links and Latte templates#56
Draft
Add basePath support to Image links and Latte templates#56
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
basePathparameter to constructor and updatedcreateLink()method to prepend the base path to generated linksIRequestparameter to constructor to extract base path from HTTP requestgetBasePath()method that extracts the base path from the HTTP request (or returns empty string if no request provided)Imageinstantiations to pass the base pathImage::createLink()now handles it automaticallyIRequestintoImageStorageconstructor via dependency injectionBasePathIntegrationTestcovering various base path scenarios (root, nested, with/without HTTP request)LatteIntegrationTestverifying that Latte macros ({img},{imgLink},n:img) correctly include base paths in rendered outputImageTestfor base path handling inImage::createLink()Implementation Details
index.phpand trailing slashImage::createLink()to include the base path, eliminating duplicationhttps://claude.ai/code/session_01FCGDD73ytpsRNwZkG4Xi5T