Route with variable

Asked

Viewed 181 times

0

I would like to know in Laravel, when saying the name of the route, it accepts the variable, instead of the name itself, as for example:

Example: Instead of being like this:

{{ route('minhaRota') }}

Be like that:

   var minhaRota = 'minhaRota'  
   {{ route( minhaRota ) }}

Only this way he gives me an undefined constant warning

I’m trying to do this with javascript

Since I want to repurpose code, and I will use the same page for other types of operations, I would not like to keep copying code to another file, I would just send a variable with the current route to finish certain operation back to where I was.

To get the current route I’m using so:

let currentUrl = '{{ Route::getCurrentRoute()->action['as'] }}'

Then I send this current url to a form so that when it finishes the operation (save, edit) it goes back to where it was before the currentUrl

  • The way you did it is like there is a constant in PHP. Javascript and PHP do not share the same variable, they are different things. Tell us what you want to do!

  • Oops, good afternoon the best way to do this is to use a lib that already exists, the name is laroute, gives a search, worth it. https://github.com/aaronlord/laroute

1 answer

0

From what I understand, you seem to be wanting to do something impossible: Calling a PHP function by passing a Javascript variable as a parameter.

But once speaking: PHP is Server-Side, Javascript is Client-Side.

var minhaRota = 'minhaRota'; // Essa variável é do Javascript
{{ route( minhaRota ) }} // Essa chamada ocorre no PHP, não no Javascript

So it’s understandable that PHP says it’s an undefined constant. By the way, PHP’s variable definition syntax is completely different from Javascript [I think you should have noticed this].

I don’t quite understand what you want to do, but if you want to take the current route, it’s not a very difficult thing to do in Laravel.

I would use the attribute data- for that. So:

<body data-route="{{ request()->route()->action['as'] or false }}"></body>

In Javascript, you can do so:

let currentUrl = document.body.dataset.route;

Observing: Of course, you’ll set this in your main layout (assuming you have one, because if you don’t, your idea of "code reuse" has already gone into space).

  • Then it will be easier to get the current url, because my body is on other screens

  • Young, but what is the problem of being on other screens? What will interfere in the functioning of other pages?

  • Note that if there is no route name, it will simply return false. It won’t change anything for you.

  • To me returned 1

  • @adventistaam 1? I don’t know if there has been any change in the version of the Laravel, because the or is a shortcut to isset. try to remove the or false and take the test.

  • Returned to the current route. How would I use my form action or not? {{ route( oRetorno ) }}

  • @adventistaam you would have to manipulate the form by Javascript, you know? You could make a document.querySelector('#id_do_form').action = currentUrl

  • He would already recognize the route?

  • Not yet......

Show 4 more comments

Browser other questions tagged

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