I m working on fresh laravel project.
I had install the laravel framework through composer then I created a route for testing purpose like this:
Route::get('/', function () {
return view('pages.home');
});
This worked fine and I got the desired page. Now for understanding middleware I added this line of code:
Route::get('/', function () {
return view('pages.home');
})->middleware('auth');
Now its throwing error of
Route [login] not defined.
What I know that, it throwing this error because I havent install any voyagers package thats why it not finding 'login'
route.
But My question is How can I change that Route [login]
to my desired page like Route [pages.notauth]
.
Please help me for this.
First run php artisan make:auth
to make the Laravel auth boilerplate. Then in the LoginController
add the the following:
class LoginController extends Controller {
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function showLoginForm() {
return view("pages.notlogin");
}
}
You're getting the message Route [login] not defined.
not because you've not installed Voyager package, but because you've not created or defined any Routes for general authentication.
Yes, if you install Voyager package, then the error message will be gone because package itself will create the necessary auth routes and controllers.
In order to do that you've to run php artisan make:auth
in command line.
In laravel 5.4, all the middlewares have been registered in the
app\Http\Kernel.php
In the file, you'll see that
protected $routeMiddleware = [ 'auth' => \\Illuminate\\Auth\\Middleware\\Authenticate::class,
All the authentication related tasks are being handled by \\Illuminate\\Auth\\Middleware\\Authenticate
class. So, if you want to change the basic behavior of the auth middleware then you've to extend this class.
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as BaseAuthenticator;
class Authenticate extends BaseAuthenticator
{
protected function authenticate(array $guards)
{
// TO DO: do your desired change
}
}
try to add
Auth::routes();
in your routes files to get rid of that error, Auth::routes()
is just a helper class that helps you generate all the routes required for user authentication.
if you want to change the url add this instead and change it as you want :
// Authentication Routes...
$this->get('login', 'Auth\LoginController@showLoginForm')->name('login');
$this->post('login', 'Auth\LoginController@login');
$this->post('logout', 'Auth\LoginController@logout')->name('logout');
// Registration Routes...
$this->get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
$this->post('register', 'Auth\RegisterController@register');
// Password Reset Routes...
$this->get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm');
$this->post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail');
$this->get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
$this->post('password/reset', 'Auth\ResetPasswordController@reset');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.