I’m using Sage 11 (with Acorn) and WordPress.
Setup
- I have a custom WordPress page template:
template-contact.blade.php - It contains a simple form:
{{--
Template Name: Contact
--}}
<form method="POST" action="{{ route('contact.send') }}">
@csrf
<input type="text" name="name">
<input type="email" name="email">
<textarea name="message"></textarea>
<button type="submit">Send</button>
</form>
- I defined a route in
routes/web.php:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ContactController;
Route::post('contact/send', [ContactController::class, 'send'])->name('contact.send');
- And in the controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ContactController extends Controller
{
public function send(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'message' => 'required|string|max:5000',
]);
return redirect()->back()->with('status', 'ok');
}
}
Problem
- If I submit the form with a missing field (e.g. no
name), the validation triggers a 302 redirect as expected. - However, after the redirect, in the
template-contact.blade.php, there’s no way to access$errorsorsession()data — the session is empty:dump(session()->all())returns[]. - Similarly,
with('status', 'ok')is not available after a successful redirect. - If I create a custom GET route and render a Blade view directly from Laravel (outside of the WordPress template), everything works perfectly: validation errors, session data, etc.
What I tried
- Wrapping the route in a
Route::middleware('web')group. - Ensuring
@csrfis in the form. - Verified
session()->put('foo', 'bar')works inside a route/controller. - Tried injecting
$errorsvia a view composer into all views. - But none of the session data appears in the WordPress-rendered Blade template (
template-contact.blade.php) after redirect.
The issue seems to be when the redirect happens, WordPress routes back to the template-contact.blade.php, but does not boot the Laravel middleware stack, so session data like $errors or with('status') are never available in the template.
Question
How can I use redirect()->back()->withErrors() or with() in a controller and still access the session data (like $errors) inside a WordPress Blade view such as template-contact.blade.php?
Or:
Is there a correct way to bridge Laravel’s redirect/session system and WordPress template rendering in Sage 11?
Thanks