active
active copied to clipboard
Can't pass url parameter
I have a marketplace url with a slug to define the category, how does one pass the parameter into the active helper?
E.g. marketplaces/cars
active('marketplace.show', $slug')
You should be able to use the route()
helper here - active(route('marketplace.show', $slug))
.
Let me know how it goes!
@dwightwatson did not work my partial looks like this.
<ul class="dropdown hover-effect">
<li class="dropdown-item {{ active('marketplaces') }}">
<a href="{{route('marketplaces')}}">All Marketplaces</a>
</li>
@foreach(\WC\Listings\Marketplace::all() as $marketplace)
<li class="dropdown-item {{ active(route('marketplace', $marketplace->slug)) }}">
<a href="{{route('marketplace', $marketplace->slug)}}">{{$marketplace->title}}</a>
</li>
@endforeach
</ul>
The link outside the loop works as expected none of the loop items active state works.
Sorry about the delay on this, been juggling a bunch of other things. I think it's expected that if you pass a URL that it will still return the active class as expected. Let me take a look.
I think I've got this working on dev-master
now - would you mind trying it out and letting me know how you go? If it's all good I'll tag a new release.
@dwightwatson I may have run into a similar issue:
Try this:
Route::get('/test/{id}', function ($id) {
return json_encode(is_active(\route('test', 8)));
})->name('test');
Playing around ids 8 and 88. If the route is in the form of /test/8?page=1 the above will fail of course. So I tried
return json_encode(is_active('test/8*'));
But then 8 and 88 will BOTH return true. I can't use (?) because that seems to also be a wildcard and they will both fail...
Any suggestions on how to proceed if the URL is in that format?
@dwightwatson I may have run into a similar issue:
Try this:
Route::get('/test/{id}', function ($id) { return json_encode(is_active(\route('test', 8))); })->name('test');
Playing around ids 8 and 88. If the route is in the form of /test/8?page=1 the above will fail of course. So I tried
return json_encode(is_active('test/8*'));
But then 8 and 88 will BOTH return true. I can't use (?) because that seems to also be a wildcard and they will both fail...
Any suggestions on how to proceed if the URL is in that format?
I eventually ended up with the more verbose solution below:
return json_encode(is_active(\route('test', 8)) || is_active(\route('test', 8) . '?q=*'));
Clearly that’s not desirable behaviour, the query string shouldn’t affect the output. Happy to take a look at any PR that can resolve it clearly.