Laravel 5 withInput old always empty

I am creating a simple login form using Laravel 5 and I would like to fill in some default logins when some error occurs. My router calls these functions as given

Route::get('/login','AuthController@login');
Route::post('/login','AuthController@do_login');

      

My controller is like below

namespace App\Http\Controllers;
use App;
use App\Models\User;
use Request;
use Illuminate\Support\Facades\Redirect;

class AuthController extends Controller {

   function login()
   {
       if(User::check())
       {
           /*User is logged, then cannot login*/
           return Redirect::back();
       }
       else
       {
           return view('auth.login');
       }
   }
    function do_login()
    {
        return view('auth.login')->withInput(Request::except("password"));
    }

}

      

Noting that inInput is underlined by PhpStorm saying it cannot find its implementation in Illuminate \ View \ View. Inside my view, I have the following:

            {!! Form::open() !!}
            <div class="form-group">
                {!! Form::text('email', Input::old('email'), array('placeholder' => Lang::get('auth.email'), 'class' => 'form-control')) !!}
            </div>
            <div class="form-group">
                {!! Form::password('password', array('placeholder' => Lang::get('auth.password'),'class'=>'form-control')) !!}
            </div>
            <div class="form-group">
                <button class="btn btn-primary btn-block">@lang('auth.signin')</button>
                <span class="pull-right"><a href="/{{App::getLocale()}}/register">@lang('auth.register')</a></span>
            </div>
        <input type="email" class="form-control" name="email" value="{{ old('email') }}">
        {!! Form::close() !!}

      

What am I missing here? Can this be done in Laravel 5? Thanks a lot for your time!

+3


source to share


7 replies


withInput()

is designed to preserve input during redirection.

It is not necessary (and not possible) to call it if you do return view()

.



Perhaps you have a do_login

do return redirect()->back()->withInput(Request::except("password"));

to implement Post / Redirect / Get correctly .

+7


source


I suggest you use the MessageBag class. First, import this namespace at the top of your controller file:

use Illuminate\Support\MessageBag;

      

Then you can use it in your controller:

$message = new MessageBag(["username" => "A default and valid username"]); // Create your message
Redirect::to('auth/login')->withErrors($message); // Return it to your view, whatever the url

      

And then in your auth / login view:



{!! Form::text('username', Input::old('username'), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}

      

You can also replace the input value if you like:

{!! Form::text('username', ($errors->has('username')?$errors->first('username'):null), array('placeholder' => ($errors->has('username')?$errors->first('username'):Lang::get('auth.username')), 'class' => 'form-control')) !!}

      

Your "username" should now display the message "Default and valid username" if an error occurs.

+2


source


Is passing data to the error object helpful advice? What if you are going to use the error object the way it intended?

Instead, there is a correct way to do it. The second parameter you pass to -> view is data! horray!

return view('your.view', dataYouWannaPass)

      

works great!

laravel 5.1 doc also recommends using only

->with($data)

      

http://laravel.com/docs/5.1/views

0


source


My solution for this problem ... Instead of putting your routes inside this

Route::group(['middleware' => ['web']], function () { });

      

Just remove it and it will work correctly. Its like "network" middleware is loaded twice.

0


source


Building on @AnthonyPillos answer,

Laravel 5.2.27 I think the need for the middleware group has been eliminated, so the elimination is happening.

So:

Route::group(['middleware' => ['web']], function () { });

      

becomes redundant.

Hope this helps someone else looking for validateRequests, validatesLogin, etc. etc., only to find out what the route was.

0


source


In Laravel 5.2, you can write return Redirect::back()->withErrors([$errors])->withInput(Input::all());

0


source


When you return one redirect request (the Position HTTP response header is not null), the session data corresponding to the InInput parameter will not be removed. Thus, the next request can access this data.

When you return a response page without requiring a redirect (the "Location" element is null), the session data corresponding to the inInput will be removed.

So, we can see that the "withInput" function only works on redirection.

0


source







All Articles