Issues arise when integrating Laravel routes with Vue Router

When attempting to invoke a Laravel route

Route::get('/logout', 'Auth\LoginController@logout');
to log out the user and redirect to the login page, I encounter an issue. It seems like when trying to redirect to this URL, nothing happens; almost as if it is calling a Vue router route that does not exist in my router.js.

My current route configuration for Vue routes is as follows:

Route::get('/{vue_capture?}', function () {
    return view('index');
})->where('vue_capture', '^(?!storage).*$');

Additionally, this is how my router.js looks:

//import ...

Vue.use(Router)

export default new Router({
    mode: 'history',
    routes: [{
        path: '/',
        component: Inicio
    }, {
        path: '/viagens/cadastrar',
        component: Cadastrar
    }, {
        path: '/viagens/listar',
        component: Listar
    }]
})

As there is no /logout route specified in my route.js, I am confused as to why the Laravel route is not being called properly.

Answer №1

Do you create your links within your vue component using router-link or href?

  • To navigate to a vue route, use router-link;
  • If you want to call a regular or laravel route, use href;

Please inform me if this solution works for you.

Answer №2

Make sure to add your vue routes above the following code:-

Auth::routes();

Answer №3

To begin, include the Laravel authentication routes in the web.php file located inside the routes folder

Auth::routes();

Next, on the front end, make a request to the logout route using axios

axios.post('/logout').then(response => {
                location.reload();
            }).catch((error) => {
               console.log(error);
            });

Answer №4

During the course of my project, I executed certain steps. One of them involved utilizing Auth::routes() in the web.php file to manage authentication by calling the logout method in AthenticatesUser.php. Please take a moment to review this PHP class.

<template>
<div class="container">
    <button class="btn" @click="logout">Logout</button>
    <form id="logout-form" action="/logout" method="POST" style="display: 
none;">
                        <input type="hidden" name="_token" :value="token">
                    </form>
    <router-view></router-view>
</div>
</template>
 <script>
  export default {
  computed: {
        token() {
            let token = document.head.querySelector('meta[name="csrf-token"]');
            return token.content
        }
    },
    methods: {
        logout() {
            document.getElementById('logout-form').submit()
        }
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hiding timestamps on an interactive transcript: A step-by-step guide

I am in the process of creating personalized interactive ebooks based on a similar website found here: This website utilizes a VTT file along with an audio file. Although the JavaScript code requires time stamps, I would like to hide them for better read ...

Endlessly triggering document.execCommand, the JavaScript selectionchange-EventListener seems to have a mind of

I recently implemented an event listener for selectionchange in the following manner: document.addEventListener("selectionchange", function() { highlight(); console.log("selectionchange-triggered"); }, false); After that, I included the code bel ...

Insert a div element into the JavaScript file

I have a JavaScript code snippet that needs to be inserted after the "Artwork" section. Here is the code: <div class="image-upload"> <label for="files"> <img src="ohtupload.jpg"> </label> </di ...

The functionality of Angular's ng-change or ng-click for select elements is not functioning properly

Exploring the concept of ng-change with checkboxes in this example. Check out the live example here Attempting to convert checkboxes into a selection but encountering some challenges. Here is the code snippet: <select> <option ng-model="confi ...

Exploring the process of gathering information using Node.js' http.request

I am currently facing a challenge where I need to call a REST API URL in one method and then use the response from that call to form a subsequent query to another URL, let's say git, to fetch some information. Despite browsing through several examples ...

Ensuring that form submissions originate from a specific URL requirement in Laravel and iframe integration

My current project involves creating a service that allows users to submit a form via an iframe on their own website. The challenge is ensuring that the form can only be submitted from the domain listed in their User model. I am aware that this is achieva ...

What is the best way to add multiple rows using a parameter in SQL?

My goal is to insert multiple rows in SQLite using the ionic framework. Inserting a single row works fine, as does running the following query: INSERT INTO categories (category_id, category_name, category_type) VALUES (1,"test",1),(2,"test again", 2); ...

Struggling to navigate to another page using Vue Router?

I'm currently in the process of integrating Vue Router into one of my projects to enable navigation between different pages. Since I'm not very familiar with Vue Router, there's a chance I may have made a mistake in the setup. import {creat ...

Issue with Angular 6 subscribe event not being caught by subject?

A new element was crafted to house a loader: @Component({ selector: 'app-loader', templateUrl: './loader.component.html', styleUrls: ['./loader.component.scss'], providers: [LoaderService] }) export class LoaderCompon ...

Combining two collections in MongoDB and calculating the total number of documents

I'm seeking guidance on working with mongodb and node.js. I currently have a collection called PIZZA { title: "bacon" } { title: "pepperoni" } as well as another collection named ORDERS { orderid: 1, pizza: "bacon" } { orderid: 2, pizza: "pepperoni ...

What could be causing the request body in ExpressJs to be blank?

Using ExpressJs, I have created a simple app that can handle post requests and form submissions. Below is the code snippet from my index.js file: require ('./models/db'); const express = require('express') const app = express() const bo ...

Having trouble getting CSS and Bootstrap to work in Laravel 5.8. Any suggestions on how to resolve this issue?

Facing a major issue while using Laravel 5.8 - unable to read CSS and Bootstrap. 1. Tried to run rm storage in order to execute php artisan storage:link, but it didn't work as expected. 2. After running php artisan serve failed, I switched to chdir( ...

Determine the minimum and maximum width of jQuery UI resizable during the "resizestart" event

As a newcomer to Javascript, I am facing challenges navigating my way around. Currently, I am attempting to create a jQuery plugin that will facilitate resizing elements using the jQuery UI resizable plugin. My goal is to implement logic that dynamically ...

What is the best way to ensure the submenu is visible on every menu when the cursor hovers over it

I am currently working on implementing a drop-down menu using react.js. I have been following a tutorial but encountered an issue when trying to add a submenu to the menu items. Instead of the submenu appearing only for the specific menu item hovered over, ...

Testing URL Parameters in JEST with an API

Seeking integration tests using Jest for an API endpoint. Here's the specific endpoint: http://localhost/universities/ucla/class/2013/studentalis/johndoe. When tested with a put request, it returns 201 on Postman. However, the testing encountered a t ...

Failure to trigger the callback for mongoose.connection.once('open') event

Currently, I am in the process of setting up a custom Node server using Next.js. Although I'm utilizing Next.js this time around, it should not affect the outcome. In my previous applications, I always relied on mongoose.connection.once('open&ap ...

Bring in multiple classes from node_modules

During the development of my package, I have organized my repository with the following structure: src - Requests.js - Constants.js package.json The package.json file contains the following information: { "name": "package-name", "version": " ...

The subsequent promise does not fire after the initial promise

Currently, I am developing a microcontroller that has the ability to accept docx files or html strings as input and convert them into a single pdf file. The converted pdf file link will then be returned as an output. This is the code snippet I have worked ...

What is the best way to save static information (such as country codes) within a Vue.js application?

After researching the most common best practices for handling this issue, I have found no clear answer. How should static content like country codes or arrays of categories be stored in a vue.js app? Storing it in my .env file as an environment variable se ...

What is the best way to extract the modal into its own file?

Greetings, fellow React developer who is new to the field! I have successfully implemented a React Class Component that includes a popup Modal. This Modal displays when a user clicks on an Item Card: import React from 'react'; import { Card, Bu ...