Web applications

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Web Applications 1 (AI1) – Lab No. 9

Laravel – other topics (to part 1)

Start of the lab:

• In XAMPP, start Apache and MySQL, then go to phpMyAdmin,

• Download the Lab009_AI1_start.zip archive to your desktop, which contains the startup project for performing tasks, and unpack this archive,

• Go to the unpacked folder and, if you have settings other than the default ones (e.g. database connections), change them in .env.example and start.…,

• Run the start.bat script (Windows, double-click) or start.sh (other systems, using the bash start.sh command).

Tasks (Laravel, N+1 problem):

Task 9.1: *

Explain the following issues:

N+1 problem,

• Eloquent ORM, two modes of loading data related by relationships,

• which causes and which prevents the N+1 problem,

• solution to the N+1 problem.

Task 9.2:

Open the cmd terminal (Command Prompt) in VSCode.

Start the php development server for using the serve artisan command.

php artisan serve

Task 9.3:

Open the second tab of the cmd terminal (Command Prompt) in VSCode.

Install the Laravel Debugbar package and check its operation.

link

composer require barryvdh/laravel-debugbar --dev

Task 9.4:

Go to the tours subpage. Go to the Queries tab in the debugbar. Explain why 8 queries are executed (1 + 7).

link

In a web browser, go to:

http://localhost:8000/auth/login


lab9

Task 9.5:

Implement the N+1 problem fix by changing the loading of the collection of trips with countries in the index function in TripController from “lazy” to “greedy”. Go back to the trips subpage. Observe the difference in the structure of the queries. Additionally, limit the download of only the necessary columns regarding countries. Observe the difference again.

link


lab9

link


lab9

Task 9.6: *

Read about other examples of the N+1 problem.

link

Tasks (Laravel, service layer):

Task 9.7:

Explain the following topics:

• business logic,

• service layer (use, advantages),

• dependency injection.

link

link

(in case of reaching the limit of articles from this portal, open links in a private window)

link

The article also has a layer of “repositories”, but instead of them, you can add new functions in models (related to a given model), which can then be used in services.

link

link

Task 9.8:

In TripController add a constructor in which to place the “injection” of the “TripService” service along with the assignment to the tripService class variable (property).

protected $tripService;
public function __construct(TripService $tripService)

{
$this->tripService = $tripService;
}

Task 9.9:

In TripService, complete the calculatePromoPrice() function, in which, based on the value of the price parameter (current price of the trip), calculate the new promotional price of the trip:

• if the current price is less than PLN 5,000, then the promotion does not occur,

• if the current price is less than PLN 10,000, then reduce the price by PLN 2,500,

• in the remaining case, reduce the price by PLN 5,000.

Return the new price of the trip.

Task 9.10:

In TripController in the show() function call the calculatePromoPrice() function to obtain the new price of the trip, which you then pass to the show view as the promoPrice variable.

Displaying the new price in the card has already been prepared.

Check the price calculations for different trips.


lab9

Tasks (Laravel, logs, middleware):

Task 9.11:

Familiarize yourself with the following issues:

• logs – logging,

• channels,

• log levels,

• current settings in the config/logging.php file,

• middleware.

Execute the following custom command (located in routes/console.php).

link

link

link

php artisan log:clear

[Task 9.12]https://github.com/dawidolko/Web-Applications/tree/main/LAB09/task():

Add the following lines to the show function in TripController.

Go to several subpages about the given trip.

Then check the contents at the end of the storage/logs/laravel.log file, and the last lines in the terminal (where the application is running).

use Illuminate\Support\Facades\Log;
Log::info('Showing details for trip: '.$id);
Log::channel('stderr')->info('Showing details for trip: '.$id);

http://localhost:8000/trips/1

http://localhost:8000/trips/4 ​​

http://localhost:8000/trips/6

Task 9.13:

Add a new lab9_errors channel in config/logging.php in channels, which will be able to save messages about errors to the lab9_errors.log file.

Task 9.14:

Go to bootstrap/app.php and change “error/exception reporting” from std_err to the channel created in the previous task. Then:

• log in as user Jan and try to remove the country USA,

• disable the MySQL database (in the XAMPP panel) and perform any action.

After that, check the contents of lab9_errors.log. Enable the MySQL database.

Task 9.15:

Using the command below, create a new middleware called LogActivity. In bootstrap/app.php, register the LogActivity middleware for the web group.

link (no Http/Kernel.php file from Laravel 10)

php artisan make:middleware LogActivity
->withMiddleware(function (Middleware $middleware) {
$middleware->web(LogActivity::class);
})

Task 9.16:

Go to app/http/Middleware/LogActivity.php and complete the handle function so that it is “user activity monitored”:

• the terminal log should show general information that someone has made a request from a given HTTP method on a given path (general form),

• the log in the laravel.log file should contain information about which user exactly made the request with a given method on a given path (specific form with parameter values),

• if the user is not logged in, the log in the laravel.log file should contain, for example, anonymous instead of the user’s email.

public function handle(Request $request, Closure $next): Response
{
Log::channel('stderr')->notice(
"Someone did {$request->method()} on '{$request->route()->uri()}'"
);
$email = $request->user() ? $request->user()->email : 'anonymous'; Log::channel('single')->notice(
"'{$email}' did {$request->method()} on '{$request->getRequestUri()}'"
);
return $next($request);
}

Task 9.17:

Check how LogActivity.php works in practice by performing different actions while being logged in as different users.

Tasks (Laravel, locale):

Task 9.18:

Install the package with ready translations: laravel-lang. Change the locale in the config/app.php file.

link

composer require laravel-lang/common --dev
php artisan lang:add pl
php artisan lang:update

link

'locale' => 'pl',

Task 9.19:

Log in to Jana. Go to edit any of the tours and test the validation (including the negative period), observe the current message translations.


lab9

Task 9.20:

Go to the lang/pl/validation.php file and complete it so that the message regarding the period is fully translated.


lab9

Task 9.21:

Go to the bootstrap/app.php file and move the message from line 29 as a value to the lang/pl.json file. Propose a key name. In the lang/en.json file, prepare the corresponding translation of the message to j.ang.

Check the content of the message for the set locales: pl and en.

link


lab9

'locale' => 'en',


lab9

Task 9.22:

Similarly to the previous task, prepare a translation of the application title: “Mountain Trips” to “Mountain Trips” and use it in templates:

head.blade.php,

navbar.blade.php,

footer.blade.php.

Similarly, check for locale: pl and en.

<footer class="container-fluid bg-body-tertiary ">
 <div class="row text-center pt-2">
 <p>&copy;  &mdash; 2024</p>
</div>
</footer>


lab9

Tasks (other issues):

Task 9.23: *

Light/dark theme - can be switched by clicking on the moon in the navbar, which using JS changes the value of the data-bs-theme attribute. Moon icon used from the Bootstrap icon set.

link

data-bs-theme="..."


lab9

link

Task 9.24: *

Remembering favorite trips within a session - ability to like/unlike a trip by clicking a heart in a given row. After the session expires/is deleted, favorite trips are lost. Heart icons used from the Bootstrap icon set.

link

link


lab9

Task 9.25: *

Error pages – for selected HTTP codes (statuses) you can create views, replacing the default view.

link

link

http://localhost:8000/trips/8


lab9

Task 9.26: *

Upload of a trip image – a form accepting several images in the format .jpg, .jpeg and saving them to disk in storage.

Validation rules include validation for several files at once and contain their own messages.

After successfully saving the files, an information toast is displayed.


lab9

link

link

link


lab9

link

link


lab9

Task 9.27: *

Transactions – …

link

To pack the project, use the archiwizacja.bat script. After unpacking the project, use start.bat again.

File version: v1.0