Route with variable

0

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

Example: Instead of being like this:

{{ route('minhaRota') }}

Look like this:

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

But so it gives me an undefined constant warning

I'm trying to do this with javascript

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

To get the current route I'm using this way:

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 currentUrl

    
asked by anonymous 10.07.2018 / 13:51

1 answer

0

So I understand, you seem to be wanting to do something impossible: Call 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. In fact, the PHP variable definition syntax is completely different from Javascript [I think you should have realized this].

I do not quite understand what you want to do, but if you want to get the current route, it's not a very difficult thing to do at Laravel.

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

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

In Javascript, you can do this:

let currentUrl = document.body.dataset.route;

Note : Of course, you will define this in your main layout (assuming you have one, because if you have not, your idea of "code reuse" has already been to space).

    
10.07.2018 / 14:11