I want to learn how csrf works. Then, I found the following website.
The teaching provided in it is: Add a function to modify the user’s name for the laravel dashboard. And this teaching is in the chapter "Set Up Simulated Functionality".
https://www.stackhawk.com/blog/laravel-csrf-protection-guide/
Create a new controller /app/Http/Controllers/UserController.php
<?php
namespace AppHttpControllers;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppModelsUser;
use IlluminateSupportFacadesSession;
class UserController extends Controller
{
public function update(Request $request)
{
$user = User::findOrFail(auth()->user()->id);
$user->name = $request->name;
$user->save();
Session::flash('message', 'Name updated!');
return back();
}
}
update /resources/views/dashboard.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
You're logged in!
</div>
{{-- This is the new code block to be added to the file --}}
@if(Session::has('message'))
<div class="bg-green-100 border-t-4 border-green-500 px-4 py-3">
<p class="text-sm">{{ Session::get('message') }}</p>
</div>
@endif
<div class="p-6 bg-white border-b border-gray-200">
<form method="POST" action="/users/">
@method('PATCH')
<div class="mt-4 max-w-xs">
<x-input value="{{ auth()->user()->name }}" id="name" class="block mt-1 w-full" type="text" name="name" placeholder="Your name here" required />
</div>
<x-button class="mt-3">
{{ __('Update Name') }}
</x-button>
</form>
</div>
{{-- End of the new code block --}}
</div>
</div>
</div>
</x-app-layout>
update routes/web.php
//add this to the top of the file
use AppHttpControllersUserController;
//This goes with the other routes
Route::patch('/users/', [UserController::class, 'update'])->middleware(['auth']);
After I added/modified the following three files according to his teaching…I got such an error message :
InvalidArgumentException
Unable to locate a class or view for component [input].
public/ index.php : 52 require_once
.
.
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture() // error
)->send();
$kernel->terminate($request, $response);
.
.
In my understanding, the code written by this master is not wrong. In addition, this is a container that uses docker to run, I don’t think it should be a version problem.
What is the reason for this error? Please how can I fix this error?
>Solution :
The error is in the view /resources/views/dashboard.blade.php,
you don’t have any components called <x-input and <x-button,
just raplace it with normal html input and button, or create missing blade component
https://laravel.com/docs/9.x/blade#components