Phalcon PhP - flash messages not showing - php

I'm having trouble showing flash messages with Phalcon PhP. Here is how I register the service:
use Phalcon\Flash\Direct as Flash;
$di->set('flash', function () {
return new Flash(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
));
});
In my controller I add the flash message like this
$this->flash->success('The carrier was successfully activated');
In my view I try to show like this (volt):
{{ flash.output() }}
My layout has the {{ content() }} tag and I have tried to apply the discussed in this post but it doesn't work anyway.
Can you see what I'm missing here? Thanks for any help!

Your are using the wrong flash session. Instead of
use Phalcon\Flash\Direct as Flash;
Use
use Phalcon\Flash\Session as Flash;
The documentation says:
Flash\Direct will directly outputs the messages passed to the
flash.
Flash\Session will temporarily store the messages in
session, then messages can be printed in the next request

Related

Laravel 5.8 redirect back with message not passing the message to the front end

I have a controller with this redirect at the end of a method:
// Setup Response message
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
return redirect()->back()->with('message', $msg);
The response looks like this:
array:5 [▼
"_flash" => array:2 [▶]
"_token" => "stMmai4OuWI2QlhYGNzxSDyb1qINLfD3RWsoM8mx"
"_previous" => array:1 [▶]
"url" => []
"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d" => 2
]
I am stumped as to what is causing the message to not be flashed through to session.
If I do this:
session()->flash('message', $msg);
dd(session()->all());
// Setup Response message
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
return redirect()->back()->with('message', $msg);
prior to the redirect, the session contains the message.
I even moved the two routes (POST and GET) outside of all Auth groups / Middleware, same result.
What could be clearing the session message?
#Udo was right, it seems a change in Laravel 5.8. This worked:
redirect()->back()->with('message', $msg);
Try it with return redirect()->back()-withErrors('message', $msg)
EDIT: You use the ErrorBag here - maybe this works as it seems something is killing your session flash.
regards
Jens
A better way of flashing data to the current session is using the global session() helper. Using this method your code should look like this:
$msg = [
'type' => 'success',
'value' => 'Your action was successful',
];
session()->put('message', $msg);
return redirect()->back();
NOTE:
Redirecting back using with(['message' => $msg]) passes the variable $message to a view or route. So you can access it using that variable name in the view you redirected to.
On the other hand, using either session()->put('message', $msg); or session()->flash('message', $msg); both flashes the data with key message into the current user session. So you can access the session data using session()->get('message')
UPDATE:
From my research, As of Laravel version 5.8, I think withInput() is now reserved for Request data (that is data pass to your controller via GET or POST request) and with() is reserved for passing data to a route or view either through route($route)->with() or view($view)->with().
So the issue with your code may be changes in the new update - version 5.8
I had the same issues, and my code looked like below, I previously had a database operation in the try cache block, I removed and the data passed was no longer working, After numerous trials, I realized I have DB transactions, so I removed them and the session/flash data redirect()->back()->with($inputArray); started working
DB::beginTransaction();
try {
if ($coupon == null) {
$this->results['failure'] = __('messages.failure_coupon_is_invalid');
return redirect()->back()->with($this->results);
}
setHasDiscount($coupon);
$this->results['success'] = __('messages.success_coupon_applied');
DB::commit();
return redirect()->back()->with($this->results);
} catch (Exception $exception) {
DB::rollBack();
$this->results['failure'] = $exception->getMessage();
return redirect()->back()->with($this->results);
}

Laracasts session->flash(); does not work Laravel Framework 5.6.3

I'm trying to flash error messages from my controller back to my view. I tried this with:
\Route::group(['middleware' => 'web'], function ()
flash('Error message');
return Redirect::back();
});
And tried showing it my view with:
#include('flash::message')
However this just seems not to show the message.
I've been looking over the web for some good 2 to 3 hours now and I am at a loss right now.
If this is a duplication of another question somewhere on stackoverflow, then sorry!
To use session flash in Laravel:
web.php
Route::get('/',
function () {
Session::flash('error', 'test');
return view('welcome');
});
In your .blade view file you can access the message using
#if (session('error'))
<div class="alert alert-warning">{{ session('error') }}</div>
#endif
You could replace 'error' with any type of message ('success', 'warning', 'yourOwnMessageIdentifier etc) you'd want to flash.
In controller
use Session;
\Session::flash('msg', 'Error' );
in blade
{!!Session::get('msg')!!}
use simply
\Session::flash('msg', 'Changes Saved.' );
#if(Session::has('msg'))
<div class="alert alert-info">
<a class="close" data-dismiss="alert">×</a>
<strong>Heads Up!</strong> {!!Session::get('msg')!!}
</div>
#endif

How to display success message without using session in laravel

Hi friend's i need help how to display success message without using session
Just adding this code before your redirect code:
$request->session()->flash('alert-success', 'User was successful added!');
Laravel 5.1 about Flash Data : http://laravel.com/docs/5.1/session#flash-data
and for your view:
<div class="flash-message">
#foreach (['danger', 'warning', 'success', 'info'] as $msg)
#if(Session::has('alert-' . $msg))
<p class="alert alert-{{ $msg }}">{{ Session::get('alert-' . $msg) }} ×</p>
#endif
#endforeach
</div> <!-- end .flash-message -->
You can use Bootstrap Alerts view: http://www.w3schools.com/bootstrap/bootstrap_alerts.asp
Append a variable name in URL like:
xyz.com/backend/packages?status=true
At your view get the url components and check if that variable exist:
if(isset($status))
{
echo '<script>alert("You message")<script>';
}
If you want to display any messages between requests without using session, you'll need to store these messages somewhere before a redirect. For example, you could use DB:
Message::create([
'message' => 'Thanks for adding the comment',
'user_id' => auth()->user()->id
]);
And get it after redirect and then delete it:
$messages = Message::where('user_id', auth()->user()->id)->get();
Message::destroy($messages->pluck('id'));
Alternatively you could use files, Redis etc as Laravel itself does when working with sessions.

Laravel 4 - Handling 404s With Custom Messages

According to Laravel 4 docs I can throw a 404 with a custom response:
App::abort(404, 'My Message');
I can then handle all of my 404s with a custom page:
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
How can I pass 'My Message' through to the view in the same way that the generic Laravel error page does.
Thanks!
You can catch your message through the Exception parameter
App::missing(function($exception)
{
$message = $exception->getMessage();
$data = array('message', $message);
return Response::view('errors.missing', $data, 404);
});
Note: The code can be reduced, I wrote it like this for the sake of clarity.
In Laravel 5, you can provide Blade views for each response code in the /resources/views/errors directory. For example a 404 error will use /resources/views/errors/404.blade.php.
What's not mentioned in the manual is that inside the view you have access to the $exception object. So you can use {{ $exception->getMessage() }} to get the message you passed into abort().

Redirect back with flash message for the layout template

In my controller I have a function to login a user.
In case the login was successful I can simply use return Redirect::back().
My problem starts when the credentials are incorrect and I want to redirect with a flash message.
I know I can chain the with method to the Redirect, but that would send the data to the specific view, and NOT to the layout, where the login HTML lies.
I could load a view like so:
$this->layout
->with('flash',$message)
->content = View::make('index');
But I need to redirect back to the referring page.
Is it possible to redirect while passing data to the layout?
The Laravel Validator class handles this quite well....
The way I usually do it is to add a conditional within my layout/view in blade...
{{ $errors->has('email') ? 'Invalid Email Address' : 'Condition is false. Can be left blank' }}
This will display a message if anything returns with an error..
Then in your validation process you have...
$rules = array(check credentials and login here...);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
return Redirect::to('login')->with_errors($validation);
}
This way...when you go to the login page, it will check for errors regardless of submission, and if it finds any, it displays your messages.
EDITED SECTION
For dealing with the Auth class..
This goes in your view...
#if (Session::has('login_errors'))
<span class="error">Username or password incorrect.</span>
#endif
Then in your auth...something along these lines..
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if ( Auth::attempt($userdata) )
{
// we are now logged in, go to home
return Redirect::to('home');
}
else
{
// auth failure! lets go back to the login
return Redirect::to('login')
->with('login_errors', true);
}

Categories