Laravel routing doesn't work - laravel

This is my route.php file:
Route::get('/users', function () {
return 'Users!';
});
But when I enter the url:
http://localhost:63342/quickstart/public/users
I get a 404 not found exception.
But if I change
Route::get('/users', function ()
to
Route::get('/', function ()
The following url functions well:
http://localhost:63342/quickstart/public/index.php

Try
Route::get('users', function () {
return 'Users!';
});

You should remove the:
/quickstart/public/
and just use:
http://localhost:63342/users

Related

Laravel API return HTML GET Request

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", ".*");

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', '.*');

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.

Get route list from parent route group in laravel

Here is the simple routing code: (i take it from official site just for example)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
});
Route::get('user/profile', function () {
});
});
Is it possible to get all routes from this group programmatically? Thank you.
Hope, this function will help you..
In Routes.php file --
Route::get('routes', function() {
\Artisan::call('route:list');
echo '<pre>'; print_r(\Artisan::output());
});

How can I redirect a route using laravel?

I use group to help visualize my project's routes.php, now the problem is
for example, when a user access to "/dir", I want to make it be redirected to "/dir/ele"
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'dir\eleController');
});
Redirect::route('ele');
}
Why is this not working?
The route dir/ele is going to a controller, but you are doing the redirect in your routes.php instead of in the controller.
You should use a closure route and do everything in the routes.php or use a controller and move the redirect to the controller:
Closure route in routes.php:
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::get('/', function() {
return Redirect::to('ele');
});
});
});
Which can be simplified to:
Route::group(['prefix' => 'dir'], function () {
Route::get('ele', function(){
return Redirect::to('ele');
});
});
Or use the controller way:
Routes.php
Route::group(['prefix' => 'dir'], function () {
Route::group(['prefix' => 'ele'], function () {
Route::controller('/', 'eleController#redirect');
});
}
eleController.php
class eleController extends BaseController {
function redirect() {
return Redirect::to('ele');
}
}

Resources