Laravel :如何使用 Request::path() 检查动态 url

Laravel : How to check dynamic url using Request::path()

我试图显示一个基于 url 的搜索框意味着对于特定路线它将显示搜索框,否则它不会显示。为此,我使用了 Request::path() 。但问题是某些路线不起作用。 假设我有两条路线,比如

Route::get('products','abcontroller@index');
Route::get('product/{name}','abcontroller@searchProduct');

现在,如果我使用以下代码:

@if(Request::path() == 'products' || Request::path() == 'product/{name}')
  // something print
@endif

对于 products 路线我可以看到搜索框但是对于 product/{name} 我不能..我该如何解决这个问题?

更好的主意是通过控制器本身通过将值传递给视图来处理这个问题。这将有助于更好的封装,因为您的布局不需要知道任何路由。

如果您默认显示搜索栏,请显示它(如果存在值)。如果仅在某些页面显示,则仅在该值实际存在时才显示。

Route::get('products',['as' => 'product.index', 'uses' => 'abcontroller@index']);
Route::get('product/{name}',['as' => 'product.name', 'uses' => 'abcontroller@searchProduct']);

使用

@if(Route::is('product.*')
// something print
@endif

希望能帮到你

使用这个:

Route::get('product/{name}',
    ['as' => 'product.name', 'uses' => 'abcontroller@searchProduct']);
@if(Route::is('product/*')
 //your Code
@endif