skip to Main Content

I’ve been trying to learn Laravel by myself and some features are bugging my mind. I’m trying to display a simple login form with just inputs and a button, so should I use function create and return a view or just declare Route::view on web.php to show a blade form?

OBS: I noticed that if the blade expect any data, I have to go through a controller, but since this won’t use it that would not be a problem.
Keep in mind I’m just trying to learning the best and professional way possible.

This is how I would code on the AuthController:

public function create() {
    return view('auth.login');
}

VS

And this is how I would declared on web.php:

    Route::controller(AuthController::class)->group(function () {
    Route::view('/login', 'auth.login')->name('login')->middleware('guest');
}

2

Answers


  1. If your route only needs to return a view, you may use the Route::view method.

    So that’s dependent on

    1-what you do.

    2- the purpose of the view.

    3-complexity of your app.

    4- data that you can pass to the view.

    5- the best practice that seniors wrote.

    you will capture the best practices by practice, build projects and see what professional engineers do

    In your case you should use controller to hanle the authentication

    see

    https://laravel.com/docs/9.x/routing
    
    https://laravel.com/docs/9.x/views
    
    https://laravel.com/docs/9.x/blade
    
    Login or Signup to reply.
  2. As Abdallah said, it depends.

    You can start this way, if your controller have no other logic than returning the view, delete the controller and simply return the view in your route file:

    Route::view('/login', 'auth.login')->name('login')->middleware('guest');
    

    If then you need a parameter, but no other logic than passing it into your view, you can use a function:

    Route::get('/login/{someParameter}', function () {
         return view('auth.login', compact('someParameter');
    })->name('login')->middleware('guest');
    

    Then you might need some more logic, at this point you may want to create a controller to handle it:

    Route::get('/login/{someParameter}',  [AuthController::class, 'myMethod'])->name('login')->middleware('guest');
    

    Then you might need some extra "guest only" pages as register. You may want to do some grouping:

    Route::middleware('guest')->group(function () {
        Route::get('/login',  [AuthController::class, 'myMethod'])->name('login');
        Route::get('/register',  [RegisterController::class, 'myMethod'])->name('register');
    });
    

    Those are only some example of what you can do with routes.
    You can refactor your route file as your project grows and try to learn more function of the routing of Laravel that may or may not help you as the time goes.

    I would also suggest to follow the naming convention for the routes. Then you won’t have to much trouble refactoring the route file if you don’t have to change the name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search