1

I was trying to show a search box based on url means for specific route it will show the search box otherwise it won't show. for that i used Request::path() . But the problem is for some route it doesn't work. Suppose i have two routes, such as

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

now if i used the following code:

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

For the products route i could see the search Box but for product/{name} I couldn't .. How do i solve the issue?

2
  • Have you checked Request::path() value? Commented Aug 8, 2017 at 10:15
  • You can get a proper solution from here! Commented Aug 8, 2017 at 11:01

3 Answers 3

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

use

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

Hope can help you

Sign up to request clarification or add additional context in comments.

Comments

2

Much better idea would be to handle this via controller itself, by passing a value to the view. That will make for better encapsulation, because your layout won't need to know about any routes.

If you display search bar by default, go with not showing it if a value is present. If it's only shown for some pages, show it only when the value is actually present.

2 Comments

I used it in view directly through Ruquest::url()
In my opinion, URL should not be used as the identifier of any page. Try to use @isset($showSearchBox) in your view to check if it exists and pass showSearchBox with true value (or anything) in any controller action that is supposed to show the view with search box. Or, like I said before, make it $hideSearchBox if you usually show it.
0

Use this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.