1

i am using form in Vuejs component template. i don't want to define full static URL. the page URL is like http://example.com/en/discusses and form should submit at http://example.com/comments. but using

<form action = "comments" method ="POST">

goes to http://example.com/en/comments instead of http://example.com/comments. How to achieve this?. Thank you in advance.

3
  • Try action="/comments" Commented Feb 9, 2018 at 10:26
  • already tried but not working. Commented Feb 9, 2018 at 10:31
  • Is your route grouped under any prefix? Commented Feb 9, 2018 at 10:37

3 Answers 3

5

You can give your post route a name in the routes file (routes/web.php):

For example:

Route::post('/comments', 'SomeController@store')->name('postComment');

Normally then, you can use the named route in your form in the blade view:

<form action="{{ route('postComment') }}" method="POST">
</form>

In this case you use a Vue component, you can pass the 'postComment' route

<component post-route="{{ route('postComment') }}"></component>

Then, accept the property in your Vue component file:

<template>
 <form action="this.post-route" method="POST"></form>
</template>

<script>
export default {
 props: ['post-route']
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I used :post-route="{{ json_encode(route('login')) }}" in blade.php and works now!
0

for ajax calls I'm using axios it has axios.default.baseUrl = 'yourBaseUrl'

yourBaseUrl is added before every ajax call You do.

Comments

0

I'm new to Vue and the above answer is exactly what I'm trying to do! I'm just not sure in which file you have used the Vue component to pass the "postComment" route:

<component post-route="{{ route('postComment') }}"></component>

Would it have its own file with passing the route as props being its only purpose? I think I am missing something..!

Thankyou!

1 Comment

If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.