In my auth middleware I have the following code:
if (! Auth::user()->enabled) {
Auth::logout();
// $request->session()->flash('Test', 'Test');
// return redirect()->route('site::home');
return redirect()->route('site::home')->withFlash('Test');
}
I'm trying to redirect the user back to the login page with a flash message but whenever I look into the session() there's no flash message.
What am I doing wrong? it feels like i've tried every variation of a flash message and none of it seems to work
return redirect()->route('site::home')->flash('This is a message!');
OR
return \Redirect::back()->withWarning( 'Message you want show in View' );
Try this instead
return redirect()->route('route_name')->with('custom_key', 'Custom Message');
Related
I want to set flash message, but flash message always missing. the flow of my controller like this:
public function actionOne()
{
Yii::$app->session->setFlash('error', 'message text');
return $this->redirect(['two', 'param1' => 'value1']);
}
public function actionTwo()
{
return $this->render('view');
}
And on the file view.php
<?php
$flashes = Yii::$app->session->getAllFlashes();
var_dump($flashes);
?>
I always get empty. How I can show flash message after redirect?
In case when you redirect to another action - You have to pass third parameter false. Link to documentation: setFlash()
Yii::$app->session->setFlash('error', 'message text', false);
In other cases, when you want to show the message in the same action you can use it like this:
Yii::$app->session->setFlash('error', 'message text'); // default third parameter is set to true.
When I submit a form in Laravel, the following controller method handles it:
public function update($id)
{
//handle input
return View::make('generic.success', ["message" => 'Data submitted successfully!']);
}
This works fine. However, instead of returning a view like above I'd like to return a redirect, because when I return the view directly, reloading the page resubmits the form.
So I tried to do this:
public function update($id)
{
//handle input
return Redirect::to('/success', ['message' => 'Data submitted successfully!']);
}
In my routes file I defined the success route:
Route::get('success', 'NotificationsController#success');
And set up a notification controller to display the view:
class NotificationsController extends BaseController {
public function success($message)
{
return View::make('generic.success', ["message" => $message]);
}
When I run the above code, I get the following error from Laravel:
InvalidArgumentException
The HTTP status code "1" is not valid.
I have no idea what this is supposed to tell me, and neither does Google apparently.
Can someone shed some light on this issue?
P.S.
Incidentally, being new to Laravel, I've noticed so far that Laravel's error reporting is very user-unfriendly, in that instead of telling me I have an issue with my router, or controller, or permissions, it displays these generic errors with no humane explanation of their cause. Is there a better way to troubleshoot problems in Laravel than relying on this?
For example, in the above incident, the error report points to this line of code...
public function setStatusCode($code, $text = null)
{
$this->statusCode = $code = (int) $code;
if ($this->isInvalid()) {
throw new \InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code));
}
...which is completely useless, as all it does is show me the code that printed the error itself.
The second parameter of the redirector's to() method is the HTTP status code that will be returned by the response, not data that will be passed along. Passing data when redirecting to GET routes can be done either via the query string or the session. The recommended solution here is to pass data via the current session using the with() method which passes that data for the next request. So in your case this would be the approach needed:
public function update($id)
{
return Redirect::to('/success')->with('message', 'Data submitted successfully!');
}
Then in your success method you can have this:
public function success($message)
{
return View::make('generic.success', ["message" => Session::get('message')]);
}
When in doubt always try checking the documentation first. The solution to this is explicitly stated in the Laravel Response Redirects Documentation.
Thanks a lot -Bogdan I found in the documentation that you post answer to my problem. In my case the solution was redirect to an action in a controller, like this...
return
\Redirect::action(
'PqrController#solicitud',
array($id)
)
->with(
'message',
'¡El estado de la solicitud ha sido actualizado correctamente!'
)
;
I redirect to a method in a controller, with one parameter array($id) and I put too in the session a message using ->with('message','Mensaje')
I want to redirect the user for particular page after successful login.
I don't want the user to navigate to the last viewed page after login.
I have tried following url but its show me error.
Error:
$credentials are required.
Laravel redirect back to original destination after login
I have changed redirect page using following code after login
Previous
return Redirect::intended('home');
Change to
return Redirect::to('home');
Add auth filter to your route and add logic to redirect user if login is success or failure.
Your route will look something like:
Route::group(array('domain'=>'a.b.com', 'before'=>'auth'), function() {
and your filter will be like:
Route::filter('auth', function()
{
if (Auth::user()->guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('account/login');
}
}
});
Have a look here for adding route and filter, and here to get basic information regarding filter. These 2 tutorials will also help:
Link 1 & Link 2
In your AccountController, try to add something inside validate function:
if (Session::has('url.intended')) {
$url = Session::get('url.intended');
Session::forget('url.intended'); // unset referring url from session
return Redirect::to($url); // redirect to referring url
}
else {
return Redirect::to('/'); // redirect to home page
}
Customize redirect location by defining a redirectPath property on the Auth/AuthController:
protected $redirectPath = '/dashboard';
I am trying to make an small app so I have choosen Fat Free Framework. I need to show some messages based on successful or error. Suppose if I want to add an user then if successfully added show message that user has been added successfully or if not show error message that user cannot be added. I cannot figure it out. Here is my UsersController code
public function index(){
$user = new User($this->db);
$this->f3->set('users',$user->all());
//there should be a way to decide if its error message or success and after display,
//it shouldn't be displayed again for the same task.
//or may be it should be check in view file, I don't know where is the correct place
// to do it
$this->f3->set('page_head','User List');
$this->f3->set('view','users/list.htm');
}
public function create(){
if($this->f3->exists('POST.create')){
$user = new User($this->db);
$user->add();
//set session here to show in view file after redirect to list page
$this->f3->reroute('/users');
} else{
$this->f3->set('page_head','Create User');
$this->f3->set('view','users/create.htm');
}
}
My flash messages controller looks like this: https://github.com/ikkez/f3-flash/blob/master/lib/flash.php
To set a message i do:
if ($this->resource->updateProperty(array('_id = ?', $params['id']), 'published', true)) {
\Flash::instance()->addMessage('Your post was published. Hurray!', 'success');
} else {
\Flash::instance()->addMessage('This Post ID was not found', 'danger');
}
$f3->reroute('/admin/post');
To render the messages i include this template in my layout https://github.com/ikkez/fabulog/blob/master/app/ui/templates/alert.html which calls a function that dumps and clears all messages, so they will only be displayed once. You can also use the SESSION in a template token like {{#SESSION.flash}} and use it for <repeat> in the template.
Working on Laravel 4, I created following route and able to fetch ID:
Route::get('details/{ID}', 'Exchange#details');
Issue is when I miss ID in route then it shows: NotFoundHttpException error. I want to avoid this. How do I proceed? Even in global.php I added following but did not work:
App::missing(function($exception)
{
print "Not Found";
// return Response::make(
// View::make('errors/404')
// , 404);
});
Controller Method
public function details($exchangeID)
{
echo "Print Details";
echo $exchangeID;
if(intval($exchangeID) == 0)
{
throw new NotFoundException;
}
}
Update: Recommendation of redirecting to 404 works but I am more interested to detect missing ID and load my Own View with proper message information instead of showing plain 404 page.
I would try something like this
# file routes.php
Route::get('{ID?}', 'SomeController#getDetails')
# file SomeController.php
function getDetails($ID=null) {
if empty($ID) {
return Redirect::to('PREVIOUS PAGE')
->withInput()
->with('error', 'invalid value');
}
}
Try like this :
App::missing(function($exception)
{
return View::make('errors.404');
});
The view is located in views/errors/404.blade.php