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]+');
Related
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", ".*");
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', '.*');
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');
I've developed an app for my homework and It's base on Vue.js, Laravel, base on tutorials, my problem is base on tutorial I've learned, I've wrote this app and now i can't access any route except my app.
I want to create a about page, and when i add route to route it's going to my default page of spa app, which, I've did it base on tutorials to prevent people to type nonsense in URL like url.com/asdasdqwe, so how to add exception to the line that's preventing me to access other routes?
Here's the code:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// I know this line make my app to force it to don't access other routes like bottom('/about')
// Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
So when i remove {path} line i'll get some problem in SPA app. so I'm looking for a way to add exception or force it let these routes work with {Path} line.
Welcome to StackOverflow :)
To resolve this, simply place your '/about' route above the one with the regex match.
Laravel processes routes in order that they are listed, so your '/about' route is not being seen because the other one is matching it first.
For example:
Auth::routes();
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('welcome');
});
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
Route::get('{path}', 'HomeController#index')->where('path', '([A-z\d-\/_.]+)?');
Route::get('dashboard/{any}', [HomeController::class, 'index'])->where(['any' => '.*']);
Change your routings sort like this:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/dashboard', 'HomeController#index')->name('dashboard');
// Because of top code i can't access other views that's using ('/etcroute')
Route::get('/about', function () {
return view('welcome');
});
Route::get('{path}',"HomeController#index")->where( 'path', '([A-z\d-\/_.]+)?' );
Laravel routing begins from the top of web.php files and checks the routings one by one.
So you have to add regex at the end of your static routes.
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.