Laravel 4 Redirect with global styling - php

Does anyone know where and how to style the redirect with global such as:
return Redirect::route('home')
->with('global', 'Your content has been posted!');
I want to use the bootstrap alerts with the global message

I just needed to put the styling after the global
Here is what I did:
return Redirect::route('admin-console-post')
->with('global', '
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
Your content has been posted successfully!
</div>
');

You need to style it in view like this:
#if(Session::has('global'))
<div class="output">
{{ Session::get('global') }}
</div>
#endif
Where output is class in which you put style of your choice.
Hope it helps ^^.

Related

Laravel 8 Redirect back with message not working in blade template

I am using Laravel v8.16.1 and PHP v7.3.1 when I am trying to get a message in the blade template.
#if(session('error'))
<div class="alert alert-success alert-dismissible">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h5><i class="icon fas fa-ban"></i> Success!</h5>
{{ session('success') }}
</div>
#endif
Using the below code, it's working for me.
$req->session()->flash('error', 'Invalid Username or Password');
return view('admin.views.login');
However, it's not the right way, and when I am trying to redirect back using the below code, then it's not working
return redirect()->back()->with('error', 'Invalid Username or Password');
Please help what is missing or what is wrong?
You can use to show errors
#if(count($errors) > 0 )
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<ul class="p-0 m-0" style="list-style: none;">
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
And the redirect code would be
return redirect()->back()->withErrors(['errors' => 'There is some error in the details.']);

Redirect to login page with message not working in laravel 5.3

When registering for external link, I want to dislay a message :
if (!Auth::check()){
Session::flash('message', trans('msg.please_create_account_before_playing', ['tournament' => $tournament->name]));
return redirect(URL::action('Auth\LoginController#login'));
}
In my login page, I have:
#if (Session::has('message'))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
I checked in my login page, with laravel debugbar, and there is no message var in session.
But it never shows up...
Any idea why it is not working?
Try dd(Session::has('message')); within the login method on your LoginController. I suspect this will return true, if so assign it to a variable of message then pass that variable to the view.
Along the lines of:
public function login() {
$message = Session::has('message') ? Session::get('message') : '';
return view('login', compact('message'));
}
Then in your view:
#if (!empty($message))
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
{{ Session::get('message') }}</div>
#endif
If the above does not work, I suspect this may be related to middleware.

Get the key of a flash message

I am working with Laravel and I am new. I jsut set a flash message by using this line of code: session()->flash('status', 'This is my flash message to display');
To retrieve the message I use session('status').
Now my question is, is there any possibility to get the key of the flash message? In my example, the key of the flash message is status
Set an array of data in session with type and message.
session()->flash('message', [
'type' => 'success',
'body' => 'This is my flash message to display'
]);
Then you can access the message type like
session('message.type')
In your blade view you can do this to have a dynamic alert message
#if (session()->has('message'))
<div class="alert alert-{{ session('message.type') }}">
{{ session('message.body') }}
</div>
#endif
You can get an array of all keys of newly flashed values using:
session('_flash.new');
Pass message like this
return redirect()->back()->with('success', 'Destination deleted successfully');
Use like this
#if(Session::has('success'))
<div class="alert alert-success alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('success') }}
</div>
#endif
#if(Session::has('error'))
<div class="alert alert-danger alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('error') }}
</div>
#endif

Laravel redirect with session message does not always persist to my request

I have an Admin tool built with Laravel where users can perform certain actions on individual records.
The way the code is built I am performing a post to a URL, executing the necessary code, updating the database and then redirecting back with a session message.
Controller Code:
return Redirect::back()->with("msg_success", "SMS has been sent!");
Now the page that gets rendered from the redirect has a check at the top to display the session message if it exists.
View Code:
#if (Session::has('msg_success'))
<div class="col-lg-10">
<div role="alert" class="alert alert-success alert-dismissible fade in">
<button aria-label="Close" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">×</span></button>
<strong>Success! </strong>{{ Session::get('msg_success') }}
</div>
</div>
<div class="clearfix"></div>
#endif
For some reason, sometimes the message is displayed and other times it is not. I cant for the life of me figure out why. Could it be middle ware that is intercepting the request and then performing another request and it is getting destroyed? I do not see anything in there that looks like it would cause this.
Any ideas would help. I thought about using a URL parameter when redirecting like
return Redirect::to('explicitURL?success=whateverthesuccesmessageis')
But I would prefer not do this as my action is shared among different URL's.
In your controller add this:
Session::flash('msg_success', 'SMS has been sent!')
return Redirect::back();
And in the View:
#if (Session::has('msg_success'))
<div class="col-lg-10">
<div role="alert" class="alert alert-success alert-dismissible fade in">
<button aria-label="Close" data-dismiss="alert" class="close" type="button"><span aria-hidden="true">×</span></button>
<strong>Success! </strong>{{ Session::get('msg_success') }}
</div>
</div>
<div class="clearfix"></div>
#endif

Using Language Class in Controller (CodeIgniter)

First of all, I made research but couldn't find anything about it.
I'm sending flash data to users when they update or add something. My controller file's related part is like this;
function hizmet_ekle()
{
if($this->mhizmetler->hizmet_ekle())
{
$this->session->set_flashdata('ok', 'hizmet sisteme eklendi!');
redirect('panel/hizmetler');
}
else
{
$this->session->set_flashdata('hata', 'Bir hata oluştu. Lütfen tekrar deneyin!');
redirect('panel/hizmetler');
}
}
And my view's related parts are like this;
<?php if($this->session->flashdata('ok')): ?>
<div class="alert alert-success fade in widget-inner">
<button type="button" class="close" data-dismiss="alert">×</button>
<i class="fa fa-check"></i> <?php echo $this->session->flashdata('ok');?>
</div>
<?php endif; if($this->session->flashdata('hata')): ?>
<div class="alert alert-danger fade in widget-inner">
<button type="button" class="close" data-dismiss="alert">×</button>
<i class="fa fa-times"></i> <?php echo $this->session->flashdata('hata');?>
</div>
<?php endif; echo validation_errors('
<div class="alert alert-danger fade in widget-inner">
<button type="button" class="close" data-dismiss="alert">×</button>
<i class="fa fa-times"></i> ', '
</div>'); ?>
My system has a few different languages, it means I have to send different notification messages(session flashdata's) for each language but I can't use lang key in my controller file. How can I handle it?
Thanks in advance.
I found it!
I have to use it like this;
$this->session->set_flashdata('ok', $this->lang->line("greek"));

Categories