Comments (4)
You should give the full Uri
into the navIsActive and use the full uri of the currentRequest
. Else you could have false active elements when having multiple webspaces and links to other webspaces in your application navigation.
E.g.:
{% set itemUrl = sulu_content_path(item.url, item.webspace) %}
{% if sulu_navigation_is_active(itemUrl) %}
return str_starts_with($itemUrl, $this->requestStack->getCurrentRequest()->getUri());
from sulu-docs.
Must say I did not know about that one but if I look at the pull request of it there are some edge cases which maybe don't work with it, example when working together with static routes or none sulu page routes. As in that case request.resourceLocator
could not be set:
Mostly a starts with
is an easier and more bulletproof solution here like its done in the demo:
So not sure if we should maybe deprecate it 🤔
from sulu-docs.
Thats what we use most of the time as well. I came across the above function while looking for a solution to shorten all my if-statements in Twig (since I noticed I was writing "app.request.uri starts with" a lot).
I ended up using something like this
public function navIsActive($itemPath): bool
{
$path = $this->requestStack->getCurrentRequest()->getPathInfo();
return str_starts_with($path, $itemPath);
}
I'm not sure if this kind of function is a Sulu-responsibility though.
from sulu-docs.
This is our current implementation, which allows either exact match (but ignoring query params) or sub path matching (e.g. when on https://www.example.com/somepath/subpath, https://www.example.com/somepath is considered active).
It also takes care of an edge case that can happen when using just a simple str_starts_with
:
- requestUrl: https://www.example.com/somepath
- targetUrl: https://www.example.com/somepath-but-not-the-same-and-not-a-subpath
final readonly class NavigationHelperTwigRuntime implements RuntimeExtensionInterface
{
public function __construct(
private RequestStack $requestStack,
) {}
public function isActiveLink(string $targetUrl, bool $allowSubPath = true): bool
{
$currentRequest = $this->requestStack->getCurrentRequest();
if (null === $currentRequest) {
return false;
}
$requestUrl =
$currentRequest->getSchemeAndHttpHost().$currentRequest->getBaseUrl().$currentRequest->getPathInfo();
return $requestUrl === $targetUrl || ($allowSubPath && $this->isSubPath(
$requestUrl,
$targetUrl,
));
}
private function isSubPath(string $requestUrl, string $targetUrl): bool
{
return str_starts_with($requestUrl, $targetUrl) && '/' === $requestUrl[strlen($targetUrl)];
}
}
from sulu-docs.
Related Issues (20)
- Website/Webspace Security is not well documented
- Add missing SuluMedia Bundle Configurations HOT 6
- Subdomains in Sulu HOT 5
- Add documentation for image format attributes HOT 1
- Redesign Issue list HOT 2
- Discussion: Community Bundles in Doc HOT 4
- Create doc how to integrate a external user provider to create sulu admin user
- The function of the <cacheLifetime> Element is described wrong
- Link Type: enable_target looks like required but it's not HOT 1
- Missing docs for text_line content type
- Improve varnish configuration for user context based caching
- User Guide for Content Managers
- Missing SuluAdminBundle Documentation
- Support for automated translation tools
- Add example vhost for caddy webserver
- Add hint how fosjsrouting works
- Incorrect documentation on content type Location HOT 1
- Basic information about sitemap is missing
- Missing documentation Webspace Portal configuration - redirect
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sulu-docs.