I am trying to setup a standard homepage that every user has access to (and lands on), before they are routed to other pages after logging in.
Currently the web.php setup is
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'home']->withoutMiddleware(['auth']));
The withoutMiddleware is to try and bypass the requirement of auth when trying to access the home.blade.php page, however I get the following error:
Very new to Laravel, so any help would be greatly appreciated. Cheers!
Routes don't require authentication unless it's explicitly specified in the routes web.php file or the controller.
So it can be as following:
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'home'];
Route::get('/', function () {
if(Auth::guest()){
return view('/home');
}else{
return redirect('/login');
}
});
Related
For example, this line exist in my web.php file. I just follow tutorials on Youtube, they did it like this.
// Show Login Form
Route::get('/login', [UserController::class, 'login'])->name('login');
So if I name the route, how could it benefits me?
You can call this route with a helper method like this:
// Generating URLs for login page
$url = route('login');
// Generating Redirects to login page
return redirect()->route('login');
return to_route('login');
Check to Laravel docs
We have a large website with many pages. Almost all of them require the user to log in. Instead of specifying "Auth" on every single page, or on every single controller, I would like to set the routes based on if the user is logged in, like this:
// in web.php
if (Auth::isLoggedIn()) {
Route::get('/', function () { return view('pages/dashboard'); });
... lots more
}
The reason I can't do this is because Auth uses sessions, and sessions are not yet initialized in web.php, since it is done as middleware which is not run yet at this point.
I'm using Laravel 8, I believe.
Thanks.
you can group the route that need the user to be logged in, then use auth middleware
for the grouped routes:
Route::middleware(['auth'])->group(function () {
Route::get('/', function () {
//
});
Route::get('/', function () { return view('pages/dashboard'); });
});
Try using Laravel's Route middleware. Route middleware can be used to only allow authenticated users to access a given route.
I'm using Laravel 8 and am having trouble with redirecting the user when they are not logged in. I have found several similar questions but they are all in older versions of laravel and none of their solutions work.
I believe I am supposed to use Route::group() to apply the redirect to all my routes. It currently looks like this:
// In web.php
Route::group(, function(){
Route::get('/', [HomeController::class, 'home'])->name('home');
Route::get('/account', [HomeController::class, 'account'])->name('account');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback');
Route::get('/help', [HomeController::class, 'help'])->name('help');
});
I'm not sure what I'm supposed to be using in my first parameter in the group function. I believe that I need it to look through either the session or a cookie to see if there is a logged user?
At the moment, a user is stopped from logging in if they are on the login page and type the wrong user/password so the authentication is working. However, if they manually type a url in, ex. ip:port/help, they will be allowed access.
I want to have all routes on the site redirect to the login page unless the user is authenticated.
My Authentication code looks like this in my LoginController.php:
public function doLogin(Request $request){
DB::connection('mysql');
$args = $request->except('_token');
// attempt to do the login
if (Auth::attempt($args)) {
// validation successful!
return redirect('home');
} else {
// validation not successful, send back to form
return redirect('login');
}
}
My route to get to the doLogin function which is called from an html form action looks like:
Route::post('authenticate', [LoginController::class, 'doLogin'])->middleware('web');
If anyone could give any solutions or lead me on the right path, that'd be amazing. Thanks!
Edit: Solution!
In the parameter I was questioning about I needed to simply add the auth middleware.
Route::group(['middleware' => 'auth'], function(){
Route::get('/', [HomeController::class, 'home'])->name('home');
Route::get('/account', [HomeController::class, 'account'])->name('account');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback');
Route::get('/help', [HomeController::class, 'help'])->name('help');
});
you simpy needo to add the auth middleware, this way:
Route::group(function(){
Route::get('/', [HomeController::class, 'home'])->name('home')->middleware('Auth');
Route::get('/account', [HomeController::class, 'account'])->name('account')->middleware('Auth');
Route::get('/feedback', [HomeController::class, 'feedback'])->name('feedback')->middleware('Auth');
Route::get('/help', [HomeController::class, 'help'])->name('help');
})->middleware('Auth');
i replace my web.php whit this code, same as my code in laravel 5.2, now im using laravel 5.5, i dont have any errors in 5.2 version.
Route::get('/home', function () {
return view('home');
});
Route::get('/register', 'registerController#index');
Route::post('/register', 'registerController#register');
Route::get('/signin', 'signinController#index');
Route::post('/login', 'signinController#login');
Route::get('/logout', ['uses'=>'signinController#logout'])->middleware('auth');
Route::get('/profile', ['uses'=>'profileController#index'])->middleware('auth');
Route::get('/updateprofile', ['uses'=>'profileController#updateprofile'])->middleware('auth');
Route::post('/updateprofile', ['uses'=>'profileController#updateprofilesave'])->middleware('auth');
Route::post('/updateprofiles', ['uses'=>'profileController#updatechannelart'])->middleware('auth');
Route::get('/changepassword', ['uses'=>'profileController#indexpassword'])->middleware('auth');
Route::post('/changepassword', ['uses'=>'profileController#changepassword'])->middleware('auth');
Route::get('/article', 'articleController#index');
Route::get('/searchuser', ['uses'=>'searchController#index']); //Untuk searching user
Route::get('/searchuserpage', ['uses'=>'searchController#searchuser']); //searching user jquery
Route::get('/photos', ['uses'=>'documentationController#indexphoto'])->middleware('auth');
then i try to access url /profile which means need authenticate first, and it show me an error InvalidArgumentException Route [login] not defined. how to solve this problem. thankyou
this is my code for Authenticate.php
public function handle($request, Closure $next)
{
if(Auth::Check()){
return $next($request);
}
else{
return redirect('/signin');
}
}
The issue comes from the fact that somewhere in your code upon instantiation you're referring to a named route called 'login' but it's not defined in your web.php file.
An example of hitting this issue is you may have a redirect pointing to this route somewhere tucked away in one of your controllers, for example:
return redirect()->route('login');
To fix this issue, apply the name to the applicable route.
Route::post('/login', 'signinController#login')->name('login');
when you call a route in your project you must define route name .
such as :
<form action:"{{route('login')}}" method="post">
and in route :
Route::post('/signin', 'signinController#index')->name('login')
This is issue with the named routes. Please make sure which all places the named routes is being used.
Route::get('/signin', 'signinController#index')->name('login')
Here, you can see I named this route login and I can call this route anywhere using route('login') helper method.
Let's say I am defining the following route in Laravel 5.3:
Route::resource('bands', 'BandController');
The default route example coming when you start a new Laravel project has the following:
Route::get('/', function () {
return view('welcome');
});
How do I make bands route to be the default one when no controller is called instead of welcome? Meaning /?
I did read docs here but didn't found anything. Any?
Place that block inside laravel/app/routes.php instead of a Controller (4.x)
Place that block inside laravel/app/Http/routes.php instead of a Controller (5.1)
Place that block inside laravel/app/routes/web.php instead of a Controller (5.3)
Route::get('/', function()
{
return view('welcome');
});
You can redirect default to anywhere you want, i.e.:
Route::get('/', function()
{
return Redirect::to( '/bands');
// OR: return Redirect::intended('/bands'); // if using authentication
});
As #Patrick mentioned, you can simply redirect to the /bands route. However, I have to warn you that the redirect will actually change the URL in the navigation pane of the web browser. I would have suggested that you just ask the home route to use the index method of your BandController as follows:
Route::get('/', ['uses'=>'BandController#index']);