Is it possible to capture the current url in the Blade view?

Asked

Viewed 5,344 times

2

I would like to capture the example url, /user/** (everything that starts with /user), I am using Laravel and Blade, using Request::url() I get localhost:8000 but I need something that can capture the routes. Thank you

2 answers

4


To get the url in the view you can do:

<p>{{Request::url()}}</p>

To check if the second segment of the url has "user":

@if(explode('/', Request::url())[3] == 'user')
    // tem '/user'
@endif

Sending this from the controller might be a better idea:

public function logout(Request $request) {
    if($request->segments()[0] == 'user') {
        // url tem como primeiro segmento "user"
    }
}

Yet another possible solution, without having tested, sorry if it doesn’t work:

if (Request::is('user/*'))
{
     // code
}
  • Excellent answer, but a suggestion, could pass this via controler not? So it would get more "clean" to the view, from my point of view some things are preferable in the controller. It’s just a suggestion.

  • Of course I did @Guilhermenascimento, I’ll add

  • @Miguel Thank you, I did a little different but following your logic, hug.

  • You’re welcome @Felipepaetzold. Glad I could help

0

I do it like this in the Valley:

//http://localhost:8000/user/teste

//Pegar o primeiro que é "user"
Request::segment(1)

//Pegar o segundo é "teste"
Request::segment(2)

It already ignores the default URL and picks up the route directly.

In case to check if it starts with "user" you would do so in Blade:

{{ Request::segment(1) == 'user' ? 'verdadeiro' : 'falso' }}

Or:

@if(Request::segment(1) == 'user')
    //Suas instruções aqui
@endif

Browser other questions tagged

You are not signed in. Login or sign up in order to post.