Vue SPA url not working with history mode enabled - laravel

I am creating a SPA using Vue js and it kind of works but I have one problem. With history mode enabled on Vue I can enter urls and go to that page using the Vue Router but when I try to login I get the literal page html. I know I can do something like auth\{vue?} but I would prefer if i didn't have to do that. I want to be able to always keep the url with no prefixes unless it is to an API request. So for example:
I have the root view:
Route::get('/{vue?}', function () {
return view('layouts.app');
})->where('vue', '[\/\w\.-]*');
and then I have the api requests:
Route::group(['prefix' => 'api'], function () {
Route::post('/login', 'Auth\\LoginController#login');
Route::post('/register', 'Auth\\RegisterController#register');
Route::get('/logout', 'Auth\\LoginController#logout');
});
but when I hit /api/login I get the return view data from /{vue?}.
I hope that makes sense and if so any help would be amazing, Thanks.

it is clearly explained here: https://kjamesy.london/work/laravel-53-vuejs-20-make-vuerouters-history-mode-play-nicely-with-laravels-routes
Short story: declare history mode to false in VueRouter and then on the Laravel side, whenever the $request->ajax() is false, the same route will always capture the request. So, we can safely define a catch-all route to redirect such traffic to the index() method of our resource controller:

Related

Adding subdomain in Laravel 9

For some reason adding subdomain route isn't working and keeps on returning my homepage instead of the page I need. I'm using Laravel, Inertiajs Vue in my app.
Here is my route:
Route::domain('webshopview.localhost:8000')->group(function () {
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
});
I want to be able to access it as webshopview.localhost:8000 but every time I try to visit this route it returns my app home page. If I do this in normal route like the below example, it works like a charm.
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
What is missing to create a subdomain for group of routes? Why it keeps returning the app homepage ignoring the subdomain
My app works locally on 'http://localhost:8000/' and 'http://127.0.0.1:8000/' and it works like that by default without me doing anything. So all I want is to add subdomain to specific routes so that I can access like this 'example.localhost:8000' or 'example.127.0.0.1:8000'. The end result is to have the route displayed like this after deployment 'https://www.subdomain.domain.com'
I even tried this and it didn't work:
Route::get('/productview', function () {
return 'First sub domain';
})->domain('productview.localhost');
now accessing 'http://productview.localhost/' returns 'This site can’t be reached' error.
I had to manually add the routes in my etc/hosts file on windows and restart the entire machine for it to take effect.

Lumen directing all routes of specific group to *

I'm developing a website with simple landing page and a few other page such as contacts and news as front end (which uses normal PHP and lumen should be sufficient bythe way) and vuejs as backend. Trying to send all get request from '/admin/' to view('admin'). This is the best I could come up with...
$router->group(['prefix' => 'admin'], function () use ($router) {
$router ->get('/{route:.*}/', function () use ($router) { return view('admin');
});
The problem is all url I do or access, all get request response I get was 404 - not found. Not even any log or anything in lumen log or even in error log apache server. Where did I do wrong?
//using lumen since I need API for vuejs operations.

Exclude conditional routes in Laravel

I'm building and application in Laravel and Vuejs where I'm having Laravel routes as below:
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->where('view', '!=', 'admin')->name('home');
I'm using Vue-router so I'm having routing in vuejs, and I'm using history mode. The problem is when I try to call /admin it generally calls HomeController#home method. even if I go deeper like /admin/dashboard it is calling the same home method. I want if admin prefix is being called then it should call HomeController#admin method.
its all okay for me please check this
Route::get('/admin/{view?}', function (){
dd('okay');
})->where('view', '(.*)')->name('admin');
Route::get('/{view?}', function(){
dd('okay1');
})->where('view', '(.*)')->name('home');
So try this
Route::get('/admin/{view?}', 'HomeController#admin')->where('view', '(.*)')->name('admin');
Route::get('/{view?}', 'HomeController#home')->where('view', '(.*)')->name('home');

POST controller for all routes in Laravel 5

I have callback button in header of my webpage, so user can send me message from every page.
How to make route for this? Something like that:
Route::post('{*}', 'PostController#callback');
It would be better if you do it via ajax.
Use middleware so you can check every request for certain post-data.
I would do it like this: create a file called MessageMiddleware.php in the directory App\Http\Middleware\
<?php namespace App\Http\Middleware;
use Closure;
class MessageMiddleware {
public function handle($request, Closure $next) {
if(isset($_POST['internal_message'])) {
// Do something so the message reaches you (db, email, whatever)
}
return $next($request);
}
}
?>
This is just a very basic version but should give you an idea.
You will not have to register any routes for this and the middleware will work for all urls the middleware is registered for.
If you want a middleware to be run during every HTTP request to your application, simply list the middleware class App\Http\Middlware\MessageMiddleware in the $middleware property of your app/Http/Kernel.php class.
The official Laravel documentation for Middlware is very extensive and does certainly not only cover authentication middleware.
Doing some try and check I found that the most simple route allows sending callback request from every page!!!
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::post('/', ['as' => 'callback', 'uses' => 'PostController#callback']);
But I don't know why. If someone know why please tell me, because I really want to know the background.
I was also trying to do it using middleware as I was advised. It was also working solution. Messages were sending from every page, but with message I got 403 error code in console every time. And of course I was trying to get rid of that.
With this simple solution it works without any errors in console.

Laravel 5.2 Session not passing

I have a simple success message on store to DB.
\Session::flash('info', 'Success! Words created');
now if I var_dump the session and return that, great.
As soon as I move to another view. Session info is gone!
I've tried all sorts, I'm on laravel 5.1.
looked into the middleware groups but i just get blank pages when adding routes in here..
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors variable available to all your views is not being utilized because it was moved from the global middleware to the web middleware group.
There are two ways to fix this:
In your kernel.php file(app/Http/Kernel.php), you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.
Wrap all your web routes with a route group and apply the web middleware to them:
Route::group(['middleware' => 'web'], function() {
// Place all your web routes here...(Cut all `Route` which are define in `Route file`, paste here)
});

Resources