Laravel 是否请求查看(在路由的帮助下)?

Laravel if request on view (with help from route)?

我在查看时提出请求,因为我想禁用某些部分和某些页面:

@if (\Request::route()->getName() != 'product.show')

这行得通,但我的问题是,我可以在我的路线上设置一个额外的参数以使我的脚本更灵活,例如:

我的路线代码是:

Route::get('/product/{slug}', ['uses' => 'ProductController@show', 'NO_SIDEBAR_PARAM' => 'true'], function ($slug) {
    return [$slug];
})->name('product.show');

所以我想创建 if request on "NO_SIDEBAR_PARAM" if is set to "true" or "false" to enable or disable elements.

方法正确吗?

我使用 middleware 来设置我希望在大多数视图中可用的视图变量。它也可能有助于解决您的问题。

class SetViewVariables
{
    /** @var SideBarBuilder */
    private $builder;

    public function __construct(SideBarBuilder $builder)
    {
        $this->builder = $builder;
    }

    public function handle($request, Closure $next)
    {
        if ($request->route()->name() == 'product.show') {
            view()->share('sidebar', '');
        } else {
            view()->share('sidebar', $this->builder->makeSideBar());
        }
    }
}

在此示例中,SideBarBuilder 是您自己的 class,用于生成边栏。我只是喜欢使用 Laravels 容器来解决这种简单的 classes.