Hi im new to Laravel and ReactJS, i have a question i think its about Routing. why i always get redirected to localhost:8000/login.
This is my Routes in web.php:
and also where is this refering to?
Route::get('/', 'HomeController@index')->name('home');
My Homecontroller contains the following code:
public function __construct() {
$this->middleware('auth');
}
public function index() {
return view('home');
}
</div>
The constructor of your controller contains a middleware, specifically the auth middleware. Laravel uses middleware to handle requests.
The auth middleware makes sure that everybody viewing the requested page is logged in. You are probably viewing the page as a unauthenticated user.
You could either try removing the middleware (removing the __construct
function) or creating an account and logging in.
Information about routing can also be found in the docs.
But in short, your routes file contains all valid urls for your application Route::get('/', 'HomeController@index')->name('home');
means, if anybody comes across the url example.com/
go to HomeController
and look at the function index
to see what to do.
In your case it returns the view home
.
From the Laravel docs.
Redirecting Unauthenticated Users
When the auth middleware detects an unauthorized user, it will either return a JSON 401 response, or, if the request was not an AJAX request, redirect the user to the login named route.You may modify this behavior by defining an unauthenticated function in your app/Exceptions/Handler.php file:
That said, you have probably issued a command php artisan make:auth
which created default views and routes for authentication. You can list all your routes with php artisan route:list
You are getting redirected to login because you are not authenticated (logged in).