Laravel如何在自定义页面(如/forums)隐藏侧边栏组件?
Hey there! Let's work through hiding that @widget('Partners::Partners') component specifically on your /forums page. Here are a few practical, easy-to-implement approaches:
1. Check the Current Route/Path Directly in the View
The quickest fix for a single page is wrapping the widget call in a conditional that checks where the user is browsing.
Using Route Name (Recommended)
If your /forums page has a named route (like forums or forums.index), use the routeIs() method to target it:
@unless(request()->routeIs('forums', 'forums.*')) @widget('Partners::Partners') @endunless
The forums.* wildcard covers any sub-routes under /forums (e.g., /forums/posts) if you need that level of coverage.
Using Request Path
If you don't have a named route, you can check the URL path directly:
@if(request()->path() !== 'forums') @widget('Partners::Partners') @endif
2. Pass a Control Variable from the Controller
For more explicit control, pass a boolean flag from your forums page controller to the view:
Step 1: Update the Controller
// In your ForumsController's index method (or relevant page method) public function index() { // Tell the view to hide the widget for this page return view('forums.index', ['showPartnersWidget' => false]); }
Step 2: Update the View
Wrap the widget call in a conditional that uses this variable (with a default of true to keep it visible on other pages):
@if($showPartnersWidget ?? true) @widget('Partners::Partners') @endif
3. Modify the Widget Component's Logic
If you have access to the PartnersWidget class (the backend for @widget('Partners::Partners')), add the visibility check directly in its render() method:
// In your PartnersWidget class (usually in app/Widgets or the package's Widgets directory) public function render() { // Skip rendering if we're on the forums page if(request()->routeIs('forums') || request()->path() === 'forums') { return ''; // Return empty string to render nothing } // Proceed with normal rendering for all other pages return view('Partners::widgets.partners'); }
This way, the widget automatically hides itself on the forums page without needing changes to your views.
4. Use a Middleware for Global Visibility Control
If you need to manage the widget's visibility across multiple pages, a middleware can set a global view variable:
Step 1: Create the Middleware
php artisan make:middleware SetWidgetVisibility
Step 2: Update the Middleware Logic
// In app/Http/Middleware/SetWidgetVisibility.php public function handle(Request $request, Closure $next) { // Set to false for forums pages, true for everything else view()->share('showPartnersWidget', !$request->routeIs('forums')); return $next($request); }
Step 3: Register the Middleware
Add the middleware to your web group in app/Http/Kernel.php:
protected $middlewareGroups = [ 'web' => [ // ... other existing middleware \App\Http\Middleware\SetWidgetVisibility::class, ], ];
Step 4: Use the Variable in Your View
Same as the controller approach:
@if($showPartnersWidget) @widget('Partners::Partners') @endif
Pick the approach that fits your workflow best! For a single page, the first method is the fastest. If you need scalability, the middleware or component modification methods work great.
内容的提问来源于stack exchange,提问作者ром Макаров

