Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lonerunner's avatar

How to get html in response session flash or redirect with messages?

The basic example would be to display a message to the user upon registration. After user input his data in registration form it gets redirect to home page and get a message of successful registration. And typical example would be:

redirect($this->redirectPath())->with('success_message', 'Thank you for registering you may now login.');

But i want to go a little further and format this message somehow to display a link and to be in two lines for example

redirect($this->redirectPath())->with('success_message', '<h3>Thank you for registering</h3> <br />You may now login with your account credentials. <br /> [login button]');

Is there a way to use html in these kind of messages or other way to display messages in html format?

0 likes
5 replies
pmall's avatar

Do not send html :

redirect($this->redirectPath())
    ->with('success_message_title', 'Thank you for registering')
    ->with('success_message', 'You may now login with your account credentials.');

Then format it when you display it in your view.

<h3>{ { session('success_message_title') } }</h3>
<p>
  { { session('success_message') } }
</p>
1 like
lonerunner's avatar

This can be one of the solutions but i want this to make as a global messages for everything login, logout, registration, activation, post published, profile saved... etc. And some of messages would have a button (bootstrap styled) some 2 buttons different style and some just a message without formatting.

This is what i have in my header.blade.php which is global template used for everything:

@if (session('success_message'))
<div class="container">
    <div class="alert alert-success alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
      <p>{{{ session('success_message') }}}</p>
    </div>
</div>
@endif

@if (session('error_message'))
<div class="container">
    <div class="alert alert-warning alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
      <p>{{{ session('error_message') }}}</p>
    </div>
</div>
@endif

If i would for example have primary button Login and secondary Reset Password in same message than i would have to have session() for each button and check if there is a session or not and format each one. If i would have 3 buttons i will need to add another session() check.

There is no way to send html in these kind of messages? Even to include another view into main view. I have searched a bit and found a docs for nested views so instead using redirect()->with() Maybe i could try something with nested views so i can pass another view into main view same as messages?

pmall's avatar

There is no way to send html in these kind of messages?

I think you can but it is super messy, get away from this.

You can pass the name of a partial with the with method.

redirect($this->redirectPath())
    ->with('response_partial', 'action.success')
    ->with('response_data', ['title' => '...', 'content' => '...']);

Then :

@if(session('response_partial'))
    @include(session('response_partial'), session('response_data'));
@endif

In your partial you can use $title and $content

1 like
lonerunner's avatar
lonerunner
OP
Best Answer
Level 1

This is maybe a rough solution and maybe not up to laravel standard coding, but it will be easy for manage i think and it does perfectly what i need it to do.

So i decided to send redirect with message and name to the view of the message so the code would look like this

return redirect($this->redirectPath())->with('message', 'messages.activationerror');

And than i can use simplest include in my headeer.blade.php template so instead i display message i put the text into include

@if (session('message'))
    @include(session('message'))
@endif 

The @include will now link to the template name inside a message and include it into main template page. Next i made view/message/activationerror.blade.php and i can code and style it any way i want so i can have different messages for different needs, example:

<div class="container">
    <div class="alert alert-warning alert-dismissable">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
      <h4>Oh snap! You got an error!</h4>
      <p>Change this and that and try again. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.</p>
      <p>
        <button type="button" class="btn btn-danger">Take this action</button>
        <button type="button" class="btn btn-default">Or do this</button>
      </p>
    
    </div>
</div>

And now since this is my test for user activation code if i enter wrong code i get the message with buttons and formatting. I got the idea from View::make('greeting')->nest('child', 'child.view'); example

1 like
godbout's avatar

The way I did:

In my controllers, sometimes I pass data, sometimes not:

$request->session()->flash('message_partial', 'messages.added');
$request->session()->flash('message_data', ['spelling' => $request->spelling]);

$request->session()->flash('message_partial', 'messages.added_demo');

In my main app layout, I make sure I cast the data to array, in case it's null:

@if (session('message_partial'))
    @include(session('message_partial'), (array) session('message_data'))
@endif

I have a layout for the messages:

<div class="alert alert-flash fade in alert-@yield('type')" role="alert">
    @yield('body')
</div>

And I extend the layout in all of my messages partials:

@extends('layouts.messages')

@section('type', 'info')

@section('body')
    Words cannot be mastered in demo mode. Please <a href="{{ url('register') }}" class="alert-link">register</a> to start recording your own words!
@endsection

Works quite well, at least if you just have a few messages, that are quite similar. Might need to find another solutions if number of flash messages grow or are too different.

Please or to participate in this conversation.