How to Redirect Url in following format:
My code:
return Redirect::to('home/viewcustomer/$cusid')
->with('status','success')
->with('message','success');
Example :
home/viewcustomer/8
You need to take care about your string quotes while using variables within string quotes. Just update your code
Redirect::to('home/viewcustomer/$cusid')
into
Redirect::to("home/viewcustomer/$cusid")
^^ ^^
Well i usually use
Session and Redirect so you have to define it in your controller
Use Session;
Use Redirect;
Then
public function example(){
Session::flash('status','Your message');
return Redirect::to('/yourRoute');
}
If you want to show your message in your view you have to do this...
#if(Session::has('status'))
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<p><strong>Success!!</strong> </p>
<ul>
<li>{{ Session::get('status') }}</li>
</ul>
</div>
#endif
Hope this helps!!
Ps: Sorry for the bad english :)
Related
I am very new to laravel and I am really terrible with routing. I want to delete the specific data but it say that route is undefined
CandidateController.php
this is my method to delete
public function destroy(Form $candidates)
{
$candidates->delete();
return redirect()->route('candidate.approve');
}
route
Route::resource('candidates', CandidateController::class);
I am using a resourse, when I go through the tutorial, it shortened my code into above. When I clicked the button delete, it says that Undefined route [candidate.approve]. Can someone help me where I went wrong?
blade
#foreach ($candidates as $candidate)
<div class="modal__content">
<div class="p-5 text-center"> <i data-feather="x-circle" class="w-16 h-16 text-theme-6 mx-auto mt-3"></i>
<form action="{{ route('candidates.destroy', $candidate->id) }}" method="POST">
#csrf
#method('DELETE')
<div class="text-3xl mt-5">Are you sure?</div>
<div class="text-gray-600 mt-2">Do you really want to delete these records? This process cannot be undone.</div>
<button type="button" data-dismiss="modal" class="button w-24 border text-gray-700 dark:border-dark-5 dark:text-gray-300 mr-1">Cancel</button>
<button type="submit" title="delete" class="button w-24 bg-theme-6 text-white" >Delete</button>
</div>
<div class="px-5 pb-8 text-center">
</div>
</div>
</form>
</div>
#endforeach
web.php
Route::get('application/approve/{id}', 'CandidateController#postApprove')->name('application');
Route::get('candidate', [CandidateController::class, 'approve'])->name('candidate.approve');
Route::resource('candidates', CandidateController::class);
Just add new Route with candidate.approve name before Route::resource.
your web.php file will be like this
Route::get('your-url', [CandidateController::class, 'approve')->name('candidate.approve');
Route::resource('candidates', CandidateController::class);
But its better to use prural for named route, like the resource controller :
candidates.create
candidates.store
...
UPDATE
Since i know the flow of the app, you should use this on controller:
return back();
Why? because when admin click Delete on modal, it will goes to another URL to delete data from DB. After delete, return back() will redirect admin to previous URL
I'm upgrading my project from CodeIgniter 3 to CodeIgniter 4,
I'm trying to display a flashdata message inside a view but unfortunately I get differents error for each method I try.
In CodeIgniter 3, I used to call something like:
<?php if ($this->session->flashdata('message')) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $this->session->flashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
I try the same in CodeIgniter 4 but I get this error:
ErrorException
Undefined property: CodeIgniter\View\View::$session
Can any one show me how to achieve this ?
Thanks in advance.
You can use session() function directly:
<?php if (session()->getFlashdata('message') !== NULL) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo session()->getFlashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
The context ($this) is an instance of the View class --> you don't have access to the Session instance directly
You can create new instance below
$session = \Config\Services::session();
In CodeIgniter 4 the new way to set Flash data $session->setFlashdata('item', 'value'); and to view $session->getFlashdata('item');
You can check it out here : Set Flash data in session in CodeIgniter
I just use another way to display a flashdata and it works fine.
In my controller, I added a new index to the data passed to the view:
$data['message'] = "Sorry, you must login first";
return view('login', $data);
Then in the view login.php I call it like this:
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
UPDATE:
I just use the markAsFlashdata() method and It works perfectly. Here's what I did in the controller just before the return method:
$_SESSION['error'] = 'Sorry, you must login first';
$session = session();
$session->markAsFlashdata('error');
Then in the view I access the flashdata using $_SESSION['error']:
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-warning" role="alert">
<?= $_SESSION['error']; ?>
</div>
<?php endif;?>
Add this line just after echo $this->section('content');
$session = \Config\Services::session();
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
For now my solution has been to create a view method in BaseController.php.
The idea is to add more "common" info into the $data array.
/* In BaseController.php */
/**
* view function replaced
*/
public function view(string $name, array $data = [], array $options = []): string
{
// Inject global data
$data = array_merge($data,["controller" => $this]);
return view($name,$data,$options);
}
/**
* Temporary message
*/
public function flash($message, $type = 'info') {
$this->session->setFlashdata('flash_message',
["message" => $message, "type" => $type]);
}
public function getFlash() {
return $this->session->getFlashdata('flash_message');
}
/* In the descendant controller */
return $this->view('products/list',['products' => $products]);
/* In the view */
<div id="messages">
<?php if($flash = $controller->getFlash()) : ?>
<?= view_cell('Base::alert', $flash); ?>
<?php endif ?>
</div>
What I try to do is to get a message in the session when redirecting back to the page from exception handler class, When I get 'PostTooLargeException' It should back to page with a message.
If statement in Handler class
public function render($request, Exception $exception)
{
//...
if ($exception instanceof PostTooLargeException) {
$test = redirect()->route('clientbank.create')->with('message', 'File too large!'); //Cannot get message
return $test;
//dump($gg);
dump(session('message'));
dd('stop');
}
// this will still show the error if there is any in your code.
return parent::render($request, $exception);
}
}
In dump(session('message')); I can see the message .
In blade page
#elseif(session('message'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
{{ session('message') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#endif
In controller
public function create()
{
dump(session('message')); <-- getting null !!!
return view('clinetbank.bank.cratebank');
}
What I try to do is use empyt(session('message') I always get an empty session.
Also, I try this questions but not work for me.
I use laravel 5.8
Any ideas, please ?.
I'm trying to add a Session success message when a User login.
I've tried adding the following to the AuthenticatesUsers.php trait postLogin():
if (Auth::attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles)->withSuccess("message");
}
I've also tried adding to the handleUserWasAuthenticated():
return redirect()->intended($this->redirectPath())->withSuccess("message");
I run composer dump-autoload after each change but it just will not flash the message in the view. I use a partial called success.blade.php and the contents are:
#if (Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>
<i class="fa fa-check-circle fa-lg fa-fw"></i> Success.
</strong>
{{ Session::get('success') }}
</div>
#endif
I think I'm missing something but I can't think what at the moment so hoping for a fresh set of eyes.
Thank you in advance.
Don't use ->withSuccess().
Use ->with('success', 'Success message'), as described in http://laravel.com/docs/5.1/responses#redirecting-with-flashed-session-data, or use the session manager. To access the session manager, you can use the Request object:
$request->session()->flash('success', 'Success message');
See http://laravel.com/docs/5.1/session#flash-data. You can also access the session manager using the Session facade:
Session::flash('success', 'Success message');
I'm returning a notice from an ajax call with
$app = JFactory::getApplication();
$app->enqueueMessage('Joomla notice', 'info');
On the front end this results in the following (note empty heading):
<div id="system-message-container">
<div id="system-message" class="alert alert-info">
<h4 class="alert-heading"></h4>
<div>
<p>Joomla notice </p>
</div>
</div>
</div>
However I want to display the notice with a heading and a dismiss button too like it does in the backend, i.e.
<div id="system-message-container">
<button type="button" class="close" data-dismiss="alert">×</button>
<div class="alert alert-info">
<h4 class="alert-heading">Info</h4>
<p>Joomla notice</p>
</div>
</div>
Is there a Joomla way to do this or do I have to come up with a work around?
The message is rendered in media/system/js/core.js by the Joomla.renderMessages function.
You may override it in your template with
jQuery(function() {
Joomla.renderMessages = function(messages) {
// copy / adapt the original function here.
}
});
Also, non-ajax messages can be customized by the html/message.php template override.
After you en-queued your message I suggest to send the message like
echo new JResponseJson($data);
JFactory::getApplication()->close();
Then you can on the client side work on the messages array like #Riccardo's solution. For example mine ajax success function looks like
success: function(responseText){
var json = jQuery.parseJSON(responseText);
Joomla.renderMessages(json.messages);
....
You can find the code here https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/libraries/dpattachments/core.php#L162