Laravel API return HTML GET Request - laravel

I'm trying to do it:
Route::get('/foo', function (Request $request) {
return response()->json('Ready');
});
I get an answer HTML :(
But POST is works:
Route::post('/bar', function (Request $request) {
return response()->json('Ready');
});
result: "Ready"
What could be the problem?
Thx.
EDIT:
ok. I use Vue 3 and Vue-router, so i changed the file web.php:
Route::get('/{any?}', function () {
return view('index'); })->where('any', '.*');
(to work SPA)
Then I changed to
Route::get('/', function () {
return view('index');
})
Now the GET request works!
But Vue-router can't work properly without this code.
Route::get('/{any?}', function () { return view('index'); })->where('any', '.*');
What should I do ? Thanks.

The problem is that you are using web.php instead of api.php routing file.
By default all closure routes from web.php are parsed to Renderable object, which renders the output to a HTML.
If you do use api.php the closure routes will be parsed to a JsonResponse.
In summary, please do use the api.php for json responses and web.php or views only responses (aka. renderables).
Finally your vue-router parser from laravel router should be set at the end of the file.
You have
Route::get('/{any?}', function () {
return view('index'); })->where('any', '.*');
});
I strongly recommend:
// All of your other routes here...
Route::view("/{any?}", "index")->where("any", ".*");

Related

Laravel any route any doesnt get executed

I have tried the following in my laravel web routes
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*');;
But the above doesnt get called. I expect whenever i try calling any route not specified to use the above but it doesnt work.
I have checked on This but still the issue persists. What could be the issue
I have also tried adding it like
///other routes above
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '*');
But still doesnt work.
You need to use regular expressions in your where condition.
https://laravel.com/docs/8.x/routing#parameters-regular-expression-constraints
In your case this should work:
Route::get('/{any}', function () {
return view('welcome');
})->whereAlphaNumeric('any');
this is equivalent to:
->where('any', '[a-zA-Z0-9]+');

How to exclude routes from route GET

New to laravel so Im trying to do something that doesn't work. I'm building a backend dashboard with login & register page, these 2 pages should have different layouts. How can I do this as the way below is not working. I'm using VUE.
Of course is {any} gonna get all so is there a way to exclude pages from this?
web.php
Route::get('/{any}', function () {
return view('layouts.vue');
})->where('any', '.*');
Route::get('/login', function () {
return view('entry.vue');
})->name('login');
You should add your route on top of route with {any}.
For example:
Route::get('/somewhere', function () {
return view('somewhere');
});
Route::get('/login', function () {
return view('entry.vue');
})->name('login');
Route::get('/{any}', function () {
return view('layouts.vue');
})->where('any', '.*');

How to add routes in an SPA using Laravel?

I'm building an SPA. In my web.php file I have this:
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*');
Which is great, and I'm using Vue-Router for routing.
But I'm trying to add Stripe payments and in a lot of these videos that I've seen or articles that I've read, they often are still working with a few .blade files as well as .vue components.
I want to add a route for a blade file called checkout.blade.php
So now I will have this in web.php:
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*');
Route::view('/checkout-now', 'checkout');
The issue is I don't see anything, just a blank white page when I go to this route, I assume it has to do with the first Route, but how do I get around this? How can I see .blade routes in my SPA?
In this case I recommend to keep all your routes before the any route.
Route::view('/checkout-now', 'checkout');
.
.
Route::get('/{any}', function () {
return view('welcome');
})->where('any', '.*'); // Must be at the end
if you just need few route for vue router
$paths = ['vue-path', 'vue-path-id'];
foreach ($paths as $path) {
Route::get($path, function () {
return view('welcome');
});
}
Route::view('/checkout-now', 'checkout');

default route for wrong url

I want to create default route for wrong url in laravel.if I have typed tst instead of test It should redirect to default url '/' is it possible to restrict wrong url in laravel
I have the routes
Route::get('/', function () {
return view('welcome');
});
Route::get('/test', function () {
return view('welcome');
});
At the end of all routes, add following route:
Route::any('{any}', function () {
return redirect()->url('/');
});
Or you can write directly like:
Route::redirect('/{any}', '/', 301);
This will take any routes except listed one and redirect to url.

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

Resources