This article explains the fundamental elements of building a web application with Laravel. In particular, it focuses on how to configure page URLs, routing rules before and after login authentication, file naming conventions, and folder structure.

How to Configure Page URLs

In Laravel, page URLs are configured using routing. This is managed in the routes/web.php file. A basic route definition looks like the following:

Route::get('/page', 'PageController@index');

 
In this example, when accessing the /page URL, the index method of PageController is called.

Routing Rules Before and After Login Authentication

In Laravel, authentication is managed using middleware. To create routes that are accessible only to authenticated users, use the auth middleware.

Route::get('/home', 'HomeController@index')->middleware('auth');

 
In this example, users must be logged in to access /home.

You can also create routes that are accessible only to unauthenticated users. For example, the login page should only be accessible to users who are not logged in.

Login Authentication Using Auth::routes()

Using Auth::routes(); in Laravel provides an easy way to set up a series of routes related to user authentication. By using this feature, routes for login, logout, registration, password reset, and more are automatically configured.

How to Use Auth::routes()

Auth::routes(); is typically added to the routes/web.php file. This automatically registers the necessary authentication routes.

// routes/web.php

Auth::routes();

 
After adding this code, Laravel provides the following routes:

  • Login (/login)
  • Logout (/logout)
  • User Registration (/register)
  • Password Reset (/password/reset)

Rules Before and After Authentication

Rules Before Authentication: These are pages accessible to users who are not logged in. Examples include the login page and the user registration page.

Route::get('/login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('/login', 'Auth\LoginController@login');
Route::get('/register', 'Auth\RegisterController@showRegistrationForm')->name('register');
Route::post('/register', 'Auth\RegisterController@register');

 
Rules After Authentication: These are pages accessible only to logged-in users. These routes are protected using middleware(‘auth’).

Route::get('/home', 'HomeController@index')->middleware('auth');

 
In this example, access to /home is restricted to authenticated users.

Customization

While Auth::routes(); is convenient, it can also be customized according to project requirements. For example, if you want to disable user registration, you can pass options as shown below:

Auth::routes(['register' => false]);

 
With this setting, routes related to user registration will not be created.

Setting Middleware in the Controller

Adding $this->middleware(‘auth’); to a controller’s constructor ensures that all actions within that controller require authentication. In this case, there is no need to individually specify middleware(‘auth’) for each route associated with that controller in routes/web.php.

By setting middleware in the controller’s constructor as shown below, all methods of that controller will only be accessible to authenticated users.

public function __construct()
{
    $this->middleware('auth');
}

 
With this configuration, for example, all actions (methods) in HomeController will only be accessible to logged-in users.

Configuration in routes/web.php

In this case, there is no need to individually specify middleware(‘auth’) in routes/web.php. In other words, the following route definition does not require additional middleware settings because authentication middleware has already been applied at the controller level.

Route::get('/home', 'HomeController@index');

  • If you set $this->middleware(‘auth’); in the controller’s constructor, all actions within that controller will require login authentication.
  • In this case, there is no need to add middleware(‘auth’) individually to each route in routes/web.php.
  • This is a convenient way to centrally manage authentication requirements at the controller level.

Point

By using Auth::routes();, you can easily implement Laravel’s powerful authentication system and effectively manage rules before and after authentication. However, these settings and routes can be customized according to your project requirements.

File Naming Conventions

In Laravel, it is recommended to follow specific naming conventions.

  • Controllers:
    Use singular form and append Controller at the end. Example: UserController
  • Models:
    Use singular form and PascalCase (CamelCase starting with an uppercase letter). Example: User
  • Views:
    Typically use kebab-case (lowercase letters and hyphens). Example: user-profile.blade.php>/li>

Folder Structure

The basic folder structure of Laravel is as follows:

  • app:
    Contains the core application code, including models and controllers.
  • resources/views:
    Contains view files (Blade templates).
  • routes:
    Contains application routes (web, api, etc.).
  • public:
    Contains public assets such as CSS, JavaScript, and images.

Point

By understanding and applying these basics, you can smoothly proceed with web application development in Laravel. Beginners are encouraged to also review the official documentation and Laravel tutorials.

Conclusion

In this article, we explained the fundamentals of web application development in Laravel. In particular, we focused on four key elements: page URL configuration, authentication routing rules, file naming conventions, and folder structure. In Laravel, page URLs are managed using routing, and middleware(‘auth’) is used to restrict routes to authenticated users only. Additionally, by utilizing Auth::routes();, login-related routes can be easily configured. File naming conventions and folder structure were also organized according to Laravel standards. By mastering these fundamentals, you can achieve efficient web application development using Laravel.

 
*Please use this information at your own discretion.*