我想了解 csrf 是如何作業的。然后,我找到了以下網站。其中提供的教學是:為laravel儀表板添加修改用戶名的功能。該教學在“設定模擬功能”一章中。
https://www.stackhawk.com/blog/laravel-csrf-protection-guide/
創建一個新的控制器 /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();
}
}
更新 /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" hljs-number">1 w-full" type="text" name="name" placeholder="Your name here" required />
</div>
<x-button hljs-number">3">
{{ __('Update Name') }}
</x-button>
</form>
</div>
{{-- End of the new code block --}}
</div>
</div>
</div>
</x-app-layout>
更新路由/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']);
我根據他的教導添加/修改了以下三個檔案后......我收到了這樣一個錯誤訊息:
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);
.
.
在我的理解中,這位高手寫的代碼并沒有錯。另外,這是一個使用docker運行的容器,我覺得應該不是版本問題。這個錯誤的原因是什么?請問我該如何解決這個錯誤?
uj5u.com熱心網友回復:
錯誤在視圖 /resources/views/dashboard.blade.php 中,
您沒有任何名為 <x-input 和 <x-button 的組件,只需將其替換為普通的 html 輸入和按鈕,或者創建缺少的刀片組件
https://laravel.com/docs/9.x/blade#components
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/512980.html
上一篇:c中的strcpy訪問沖突
