Is there a way to generate url from route name with javascript?

Posted 1 year ago by malhayek

I am aware that route naming is something that is handled at the server side not the client. However, I hate the idea of having to hard code my urls is my JS file which defeats the purpose of using route naming.

Sine I am using mixer/webpack, I am hoping there is some kind of helper that will fetch and generate the url by giving it the route name.

I am thinking of something like this

$.ajax({
        url: resolve_url('my_laravel_route_name'),
        method: 'POST',
        data: {},
        dataType: 'JSON'
    });

instead of hard coding the url

$.ajax({
        url: 'some_url/for/my/api',
        method: 'POST',
        data: {},
        dataType: 'JSON'
    });

Is there some something like this?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.