Tasks studies - laboratory
Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko
Web Applications 1 (AI1) – Lab 11
Laravel, REST API –
Implementing a CRUD Controller (Part 1)
Starting the Lab:
- In XAMPP, start Apache and MySQL, then go to phpMyAdmin.
- Download the archive
Lab011_AI1_start.zipto your desktop, which contains the starter project for the tasks, and extract the archive. - Navigate to the extracted folder and modify
.env.exampleandstart...files if you have different settings (e.g., database connection). - Run the script:
start.bat(Windows, double-click)start.sh(other systems, runbash start.sh).
- Install Postman and skip account creation: lightweight API client.
- Install the REST Client extension.
Tasks (Laravel)
Task 11.1
Understand the concepts from the last task of the previous lab.
Review the existing modifications:
- Disabled sessions, cookies, and CSRF tokens (…_create_users_table.php migration and bootstrap/app.php).
- Added
api.phpfile for API routes/endpoints. - Removed Laravel Sanctum (authentication is not required for this lab; JWT will be used in Lab 13).
- Created a middleware to add the header
Accept: 'application/json'to every request to prevent rendering views when errors occur.
(There are differences between Laravel 10.x and 11.x)
- Middleware in Laravel 11
- Disabling HTTP sessions for API speedup
- Prevent Laravel 11 from setting any cookies
- Laravel 11 Routing Changes
Commands:
php artisan install:api
composer remove laravel/sanctum
php artisan make:middleware ForceJsonResponse
Task 11.2
Open the cmd terminal in VSCode.
Start Laravel’s development server using:
php artisan serve
Task 11.3
Open a second terminal tab in VSCode.
Run the following command to generate a new controller with CRUD operations.
This controller will exclude two unnecessary functions (explain why they are not needed).
php artisan make:controller CountryController --api --resource
Task 11.4
Set up routing for CountryController (since it is an API controller).
Modify routes/api.php and check configured routes.
Route::apiResource('countries', CountryController::class);
Expected Routes:
GET|HEAD api/countries → countries.index › CountryController@index
POST api/countries → countries.store › CountryController@store
GET|HEAD api/countries/{country} → countries.show › CountryController@show
PUT|PATCH api/countries/{country} → countries.update › CountryController@update
DELETE api/countries/{country} → countries.destroy › CountryController@destroy
Task 11.5
Implement the index function in CountryController to return all countries.
Test the result using:
- Chrome/Edge/Opera
- Firefox
- GET request in Postman
- REST Client extension in VSCode
public function index()
{
$countries = DB::table('countries')->get();
return $countries; // Status 200
}

Task 11.6
Implement CRUD functions manually, without using:
- Resources (
Resource,Collection) - Form Requests (
StoreRequest,UpdateRequest) - Model binding (
$country) - Eloquent model functions, instead use DB facade (
DB::...)
Future labs will introduce these features.
Task 11.7
Implement CountryController methods for successful cases.
Test using HTTP requests.
CRUD Implementation:
public function index()
{
return response()->json(DB::table('countries')->get(), 200);
}
public function show($id)
{
$country = DB::table('countries')->find($id);
return response()->json($country, 200);
}
public function store(Request $request)
{
$id = DB::table('countries')->insertGetId([
'name' => $request->input('name'),
'code' => $request->input('code'),
'currency' => $request->input('currency'),
'area' => $request->input('area'),
'language' => $request->input('language')
]);
$country = DB::table('countries')->find($id);
return response()->json($country, 201);
}
public function update(Request $request, $id)
{
DB::table('countries')->where('id', $id)->update([
'name' => $request->input('name'),
'code' => $request->input('code'),
'currency' => $request->input('currency'),
'area' => $request->input('area'),
'language' => $request->input('language')
]);
$country = DB::table('countries')->find($id);
return response()->json($country, 200);
}
public function destroy($id)
{
DB::table('countries')->where('id', $id)->delete();
return response()->json(null, 204);
}
Task 11.8
Modify CountryController to handle error cases (e.g., missing country, validation errors).
Example validation rules:
$rules = [
'name' => 'required|string|unique:countries,name|max:50',
'code' => 'required|string|unique:countries,code|max:3',
'currency' => 'required|string|max:30',
'area' => 'required|integer|min:0',
'language' => 'required|string|max:50',
];
$validator = Validator::make($inputs, $rules);
if ($validator->fails()) {
return response($validator->errors(), 422);
}
Task 11.9
Modify response format by removing unnecessary fields:
public function index()
{
$countries = DB::table('countries')->get();
return response()->json($countries->map(function ($country) {
return [
'id' => $country->id,
'code' => $country->code,
'currency' => $country->currency,
'area' => $country->area,
'language' => $country->language
];
}), 200);
}