Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 how to use get parameter from url with controller's sign in method

I'm trying to implement a login system where the user will be redirect back only if there is a get parameter in the url, else it will be redirect to the profile page.

So, if the uri is something like this (no get parameter)

/login

if success, the user will be redirect to the profile page.

But if the uri has the get parameter like for example

/login?r=articles

the user will be redirected to the articles page instead of the default route to the profile page.

Question is, in the controller, how can do this, if possible, or how can I check for the get parameter?

routes.php

// Signin
Route::post('/account/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'user.signin',
]);

UserController.php

// Signin
public function postSignIn(Request $request)
{
    $this->validate($request, [
        'login-email' => 'required|email',
        'login-password' => 'required'
    ]);

    if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
    {

        // Tried this, isn't working... (maybe something's missing ??)
            $redirect = $request->input('r');
            if ($redirect) {
                return redirect()->route($redirect);
            }
        // -->

        return redirect()->route('user.account');
    }
    return redirect()->back();
}

signin.blade.php

<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">

    <div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
        <label class="sr-only" for="email">Email</label>
        <input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
    </div>

    <div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
        <label class="sr-only" for="password">Password</label>
        <input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
    </div>

    <div class="form-group">
        <input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
        Remember
    </div>

    <button type="submit" class="btn">Sign in!</button>

    <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

Thanks.

Updated

Thank you all for your replies, the fact is that I'm still getting to know Laravel and that's probably why I can't implement it right the solutions that you guys shared.

So this said, I got it working by creating a conditional hidden field that holds the query value and this way once the user submits the form, it will be passed with the rest of the $response arguments.

signin.blade.php

<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">

    <div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
        <label class="sr-only" for="email">Email</label>
        <input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
    </div>

    <div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
        <label class="sr-only" for="password">Password</label>
        <input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
    </div>

    <div class="form-group">
        <input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
        Remember
    </div>

    <button type="submit" class="btn">Sign in!</button>

    <input type="hidden" name="_token" value="{{ Session::token() }}">
    <!-- Verify condition -->
    @if(isset($_GET['referer']))
        <input type="hidden" name="referer" value="{{ $_GET['referer'] }}">
    @endif
</form>

UserController.php

// Signin
public function postSignIn(Request $request)
{
    $this->validate($request, [
        'login-email' => 'required|email',
        'login-password' => 'required'
    ]);

    if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
    {

        // Check for the new argument 'referer'
        if (isset($request->referer)) {
            return redirect()->route($request->referer);
        }
        // -->

        return redirect()->route('user.account');
    }
    return redirect()->back();
}

Like so, it works.

Don't know if it's a viable and secure way to do it in Laravel 5, but it is working.

like image 781
Bruno Avatar asked Sep 02 '25 05:09

Bruno


2 Answers

When you have an URI such as login?r=articles, you can retrieve articles like this:

request()->r

You can also use request()->has('r') to determine if it's present in the URI.

And request()->filled('r') to find out if it's present in the URI and its value is not empty.

like image 60
user2094178 Avatar answered Sep 05 '25 00:09

user2094178


// only query

$query_array = $request->query();

or

$query = $request->query('r');

// Without Query String...

$url = $request->url();

// With Query String...

$url = $request->fullUrl();
like image 26
Laravel User Avatar answered Sep 05 '25 00:09

Laravel User