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.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.