Daniel-Pablo's avatar

@currentURL question

Hi , I want to know if there is a helper that acts like a @IF blade component in this case something like this

@url(home)
@endurl

So I want this to compare the current URL against the url provided in this case home? If there is no helper blade method to do this how I can create one for my app

want to achive this

@if(Request::url() === 'your url here')
    // code
@endif

BUT WITH MY URL new helper

thanks in advance

0 likes
7 replies
MichalOravec's avatar
Level 75

Check this out

https://laravel.com/docs/8.x/blade#custom-if-statements

use Illuminate\Support\Facades\Blade;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Blade::if('url', function ($value) {
        return request()->url() === $value;
    });
}

But better is to use named routes and compare it with Route::is('route-name')

1 like
Daniel-Pablo's avatar

Wonderful, I miss specifying that I was going to match the blade @url with the name of the route end up with this thanks to you

        Blade::if('url', function ($value) {
           return Route::currentRouteName() === $value;
     });

thanks, you make the solution to this question 5 stars for you

newbie360's avatar

sometimes use route name is not true, for example in sidebar want to check the item is active, but the route like this

Route::get('/{item?}', 'TestController@index')
    ->where('item', '(laravel|php|javascript)')->name('index');

all route is named index

Daniel-Pablo's avatar

@snapey please can you specify what is BAD DESIGN, you refer to this code?

        Blade::if('url', function ($value) {
           return Route::currentRouteName() === $value;
     });

if YES please let me know why and how to improve it, please I am learning a lot from the pros

Snapey's avatar

Sorry, I was replying to @newbie360

God I hate this p***ing threaded discussion. Much simpler when it was just chronological.

newbie360's avatar

but in a simple project, should we really need to split one route one controller / method, and create class for code sharing ?

in this example

Route::get('/{item?}', 'TestController@index')
    ->where('item', '(laravel|php|javascript)')->name('index');

in the sidebar we still able use this for check the item is active

request()->path()

request()->segments()

i want to learn more ;)

Please or to participate in this conversation.