URL route from Laravel to Vue Router - laravel

I'm using Laravel 5.4 and vue-js 2.4
Laravel routes
Route::get('/', 'HomeController#home');
HomeController
public function home(Request $request) {
return view('app');
}
Vue router
const routes = [
{ path: '/', name: 'home', component: App},
{ path: '/about', component: About },
];
When I click on this, about component is well displayed and the url is example.com/about
<router-link to="/about">About</router-link>
Issue
When I enter example.com/about, I'm redirected to the home page of my vue app which is example.com.
What should I do so the about can reach vue-router ?

Route::get('/{component?}', 'HomeController#home');
Seems to be working

Add a route to your about page on laravel just like / route:
Route::get('/', 'HomeController#home');
Route::get('/about', 'HomeController#home');

Related

Laravel 6: disable all routes for guest except home and login

I need to disable all routes for guests in Laravel except '/' and 'login' pages.
Does that possible to implement it routes/web.php ?
Yes. In your routes/web.php file, make sure to define your protected routes under the auth middleware group.
routes/web.php
Route::get('/', function() {
// / route
});
Route::get('/login', function() {
// login page
});
Route::middleware(['auth'])->group(function () {
// define your routes here
// they'll be protected
});
Official documentation
Since Laravel 7.7 you can use excluded_middleware property eg:
Route::group([
'excluded_middleware' => ['auth'],
], function () {
Route::get('/', 'HomeController#index');
...
});

Laravel VueJs SPA : should VueJS router routes match with Laravel routes

The question is pretty simple.
in order to build a Simple Page Application (SPA) with Laravel and VueJS , should all the paths present in router.js ( the file defining paths for SPA) be also present in web.php of Laravel ?
For example I am using the source code from here :
router.js inside resources/assets/js/frontend has :
export default [
{ path: '/create', component: SubmitTicketFormComponent },
{ path: '/view/:ticketId', name: 'client-view-ticket', component: ViewTicketComponent, props: true }
];
But the web.php inside routes folder has
Route::get('/', function () {
return view('welcome');
});
Route::get('/admin', function () {
return view('admin');
});
Route::post('/login', 'Auth\LoginController#login');
Route::post('/logout', 'Auth\LoginController#logout')->name('logout');
//Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
And the api.php inside routes folder has :
Route::middleware('auth:api')->group(function() {
Route::get('/profile', 'ProfileController#index');
Route::apiResource('/admin/tickets', 'Admin\TicketsController');
});
Route::apiResource('tickets', 'TicketsController');
So what are the rules regarding the path or route declaration for both VueJs and Laravel ?
If you want to have a SPA and keep some of the Laravel pages you need to have a route like this:
Route::get('/{any}', 'VueController#index')->where('any', '.*');
This can be at the end of your routing file, so if it does not match the existing Laravel routes and it will open the Vue SPA and the routing will be handled from there.

Laravel 5.5 all routes except home result in 404 not found

Just trying out laravel 5.5 which I installed in cpanel. Only the root route is working:
Route::get('/', function () {
return view('public/content/home-content');
});
Even this isn't working
Route::get('/about', function () {
return view('public/content/about-content');
});
It also work:
www.mydomain/index.php/about
How can I solve this error

Laravel - Middleware triggered while it should not?

I'm using Laravel 5.4 and Vuejs 2.3 along with VueRouter
I have a website that consists of two parts
example.com
example.com/tools
Problem
If I am at example.com/tools and if I reload the page to the same address example.com/tools the middleware checkUsersPublic is triggered whereas it is only linked to /.
Edit
If I remove { path: '/tools', component: Stats } from VueRouter.js the middleware is not triggered anymore
Question
What should I do change so the middleware is not triggered ?
Routes in Laravel
Route::get('/', 'HomeController#home')->middleware('checkUserPublic')
Route::get('/tools', 'StatsController#home')->middleware('checkUserStats')
Vue router
const routes = [
{ path: '/', component: App },
{ path: '/tools', component: Stats },
];
Middleware checkUsersPublic
class checkUsersPublic
{
public function handle($request, Closure $next)
{
if(auth()->check() && auth()->user()->public_only) {
return $next($request);
}
if(Auth::check()) {Auth::logout();}
return $next($request);
}
}
Instead of doing the middleware in Laravel I did it within vue-router in the beforeEnter guard

LARAVEL 5.2 - How to access login page as default page

I just need to know how i can set the login page as default page of my site.
Route::get('/', function () {
return view('welcome');
});
I need something like this:
Route::get('/', function () {
return view('auth.login');
});
It's possible?
Thanks
Yes, it's possible, just put this route before all other routes and this will work.
If you're using standard Laravel 5.2 auth system, you can create this route and use it before all other routes:
Route::get('/', 'Auth\AuthController#getLogin');

Resources