php - 如何将值从输入文本框传递到 Laravel 中的另一个页面
问题描述
索引控制器如下图所示
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class IndexController extends Controller
{
public function index()
{
return view('welcome');
}
public function about()
{
return view('about');
}
public function contact()
{
return view('contact');
}
public function thanks(Request $request)
{
$request->validate(['firstname' => ['required', 'alpha_num']]);
return view('thanks');
}
}
contact.blade.php 在下面
@extends('welcome')
@section('content')
<div>
<h2>Contact Page</h2>
<form method="post" action="{{route("thanks")}}">
@csrf<input type="text" name="firstname"/>
<input type="submit"/>
</form>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
@endsection
谢谢.blade.php
@extends('welcome')
@section('content')
<div>
<h2>Thanks</h2>Thank you {{ $firstname }}
</div>
@endsection
欢迎.blade.php
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
app
</div>
<div class="links">
<a href="{{ route('about') }}">About</a>
<a href="{{ route('contact') }}">contact</a>
</div>
<h1>{{$firstname}}</h1>
<div>
@yield('content')
</div>
</div>
</div>
网页.php
<?php
Route::get('/', 'IndexController@index');
Route::get('/about', 'IndexController@about')->name('about');
Route::get('/contact','IndexController@contact')->name('contact');
Route::post('/contact','IndexController@thanks')->name('thanks');
当我单击welcome.blade.php 中的联系人时,我会转到文本框所在的联系人页面。输入的值应该出现在thankyou.blade.php 中。当我单击提交时,我需要在文本框中输入的值显示在 Thanks.blade.php 中。提前致谢
解决方案
数据需要显式传递给视图。出于希望显而易见的原因,它不能作为变量全局访问。
例如
return view('thanks', ['firstname' => 'Your First Name']);