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
Related
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');
}
});
Hello i want to know that how can i access the current username in my laravel route file? i want to add current user name as prefix in my url but i don't want to pass username as parameter for every route call because project is already completed so its is a complex and time consuming task. so please suggest how can I get current username.
Here is my code:
Route::prefix(/Auth::user()->name)->group(function () {
Route::get('/test', 'HomeController#test')->name('test');
});
<a href="{{route('test')}}">
Thanks in advance.
Note:- I don't want to pass username from templates or controller because its complex and time consuming as project is already done.
You can not pass Auth user from routes/web.php , you need to pass paramter in your route .
Route::group(['middleware'=>'auth','prefix'=>'/'],function () {
Route::get({username}, 'HomeController#test');
});
in your controller :
public function test(Request $request,$username){
$user=User::where('name',$username)->first();
if($user){
return response()->json((array('status'=>'success','user'=>$user));
}else{
return response()->json(array('status'=>'error','message'=>'user not found'));
}
}
Routes are registered before Session is initialized. So you can't access the authenticated user in the routes file.
So you have to register new routes with a dummy username prefix as,
Route::prefix('{username}')->group(function () {
Route::get('/test', 'HomeController#index');
});
Just add a param {username} & no need it use anywahere. Keep this route group at the end of the routes file.
Re-generate your routes as,
<a href="{{ url(auth()->user()->name . '/test') }}">
I have created a laravel project. My login URL is like that:
http://localhost/project_name/public/login
I want to change the route name login to user and registration route to new-user. How can I do that?
You could write the routes yourself inside your routes/web.php file:
Route::get('user', 'Auth\LoginController#showLoginForm');
Route::get('new-user', 'Auth\RegisterController#showRegistrationForm');
These will be created next to your original login routes if you use Auth::routes(); inside your routes/web.php.
If you want to remove the Auth::routes(); you can offcourse, but you will have to add the post methods as well from the original login routes. You can check where they are going by doing php artisan route:list in your terminal to figure out how to point these and to which method.
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.
i want change laravel login url from /auth/login to login
already make change in AuthController
protected $loginPath = 'login';
and routes
Route::get('login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('logout', 'Auth\AuthController#getLogout');
i can access login page if manually go to that page
but if redirect to login page after trying to access page already protected with Middleware its still redirect to /auth/login
Navigate to the middleware folder app\Http\Middleware. Edit the Authenticate file
return redirect()->guest('auth/login');
to be
return redirect()->guest('login');
You can do this:
Route::get('your_custom/login_path', [
'as' => 'login',
'uses' => 'Auth\LoginController#showLoginForm'
]);
You don't need to add or modify any other file. Here is why:
As of writing this answer, Laravel is at 5.4 mark. At this point you no longer have $loginPath.
The guys at Laravel also did a nice job in updating the platform as in they replace
return redirect()->guest('login');
with
return redirect()->guest(route('login'));
which makes really easy overriding routes because their pointed by their names now (and not hard coded).
(this is a bonus tip - for security - limiting the number of attempt logins) Just added it at the end.
->middleware('throttle:3,1');