Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VueJs page gives 404 on refresh

Tags:

laravel

vuejs2

I just uploaded my blog app to a test server. When I click on post from the homepage I am able to view the single page using the slug URL. However, from the single page, If I refresh the page it gives me a 404 page.

I had used something like the below for my users component:

Route::get('/admin/users{vue_capture?}', function () {
    return view('admin');
})->where('vue_capture','[\/\w\.-]*');

This is how my routes.js looks like:

import VueRouter from 'vue-router';
import Admin from './components/Admin.vue';
import Homepage from './components/Homepage.vue';
import Subscriber from './components/subscriber/Main.vue';
import Single from './components/Single.vue';
import Categories from './components/Categories.vue';

let routes = [
    {
        path: '/',
        component: Homepage
    },
    {
        path: '/subscriber',
        component: Subscriber
    },
    {
        path: '/post/:slug',
        name: 'post',    
        component: Single
    },
    {
        path: '/categories/:slug',
        component: Categories
    }
];

export default new VueRouter({
    mode: 'history',
    routes
});

And my Laravel routes are like this:

<?php
use App\User;
use App\Http\Resources\User as UserResource;
use App\Http\Resources\UserCollection;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('homepage');
});

Auth::routes();
Route::get('/logout', 'Auth\LoginController@logout')->name('logout' );
Route::get('/home', 'HomeController@index')->name('home');

/************************************************************
                            API'S
************************************************************/
Route::get('/api/homeposts', 'HomePostsController@posts')->name('homeposts');
Route::resource('/api/users', 'AdminUsersController');
Route::resource('/api/posts', 'AdminPostsController');
Route::resource('/api/categories', 'AdminCategories');
Route::get('/api/loggedUser', 'AdminUsersController@loggUser')->name('singleUser');
Route::put('/api/updatedUser/{id}', 'AdminUsersController@updateUser')->name('singleUserUpdate');

Route::resource('/api/subscriber', 'SubscriberController');
/*Route::get('/admin/users{vue_capture?}', function () {
    return view('admin');
})->where('vue_capture','[\/\w\.-]*');*/

/************************************************************
                        ADMIN ROUTES
************************************************************/
Route::group(['middleware'=>'admin'], function(){
    Route::get('/admin/users', 'AdminUsersController@users')->name('allusers');
    Route::resource('/admin/posts', 'AdminPostsController');
});

/************************************************************
                    SUBSCRIBER ROUTE
************************************************************/
Route::group(['middleware'=>'subscriber'], function(){
    Route::get('/subscriber/{vue_capture?}', function () {
        return view('homepage');
    })->where('vue_capture','[\/\w\.-]*');
});

/**********************************************************/

Route::get('/single/post/{slug}', 'AdminPostsController@single');

But how exactly can I handle for the single post page?

like image 704
user agent Avatar asked Jul 18 '18 10:07

user agent


2 Answers

Assuming your entry point is the index method of your HomeController, you just need to add this route at the bottom of your routes/web.php file.

Route::get('{any}', function () {
    return view('homepage');
})->where('any','.*');
like image 93
Julian Paolo Dayag Avatar answered Nov 17 '22 20:11

Julian Paolo Dayag


This isn't an issue with Vue.

When I visit the webpage http://blog.sidneylab.com/post/quasi-non-est-magnam-quae-aut-omnis-nisi (the first post on your website), I get a 404 error from the server itself. Same with anything else on /post/

That means your route handler isn't sending the relevant Vue index or code when a route matches. Ensure it does this with either the /post/ URL or just add a catch-all handler at the bottom of your default route handler:

Route::get("{any}", "DefaultHandler@myMethod")->where("any", ".*");
like image 2
Sam Holmes Avatar answered Nov 17 '22 18:11

Sam Holmes